zoukankan      html  css  js  c++  java
  • Object.wait()与Object.notify()的用法

    Object.wait()与Object.notify()的用法

     

    参考文献:

    object.wait()和object.notify()和object.notifyall()

    正文

    wait、notify和notifyAll方法是Object类的final native方法。所以这些方法不能被子类重写,Object类是所有类的超类,因此在程序中有以下三种形式调用wait等方法。

    wait();//方式1:
    this.wait();//方式2:
    super.wait();//方式3

    void notifyAll()

    解除所有那些在该对象上调用wait方法的线程的阻塞状态。该方法只能在同步方法同步块内部调用。如果当前线程不是锁的持有者,该方法抛出一个IllegalMonitorStateException异常。

    void notify()

    随机选择一个在该对象上调用wait方法的线程,解除其阻塞状态。该方法只能在同步方法同步块内部调用。如果当前线程不是锁的持有者,该方法抛出一个IllegalMonitorStateException异常。

    void wait()

    导致线程进入等待状态,直到它被其他线程通过notify()或者notifyAll唤醒。该方法只能在同步方法中调用。如果当前线程不是锁的持有者,该方法抛出一个IllegalMonitorStateException异常。

    void wait(long millis)和void wait(long millis,int nanos)

    导致线程进入等待状态直到它被通知或者经过指定的时间。这些方法只能在同步方法中调用。如果当前线程不是锁的持有者,该方法抛出一个IllegalMonitorStateException异常。

    Object.wait()和Object.notify()和Object.notifyall()必须写在synchronized方法内部或者synchronized块内部,这是因为:这几个方法要求当前正在运行object.wait()方法的线程拥有object的对象锁。即使你确实知道当前上下文线程确实拥有了对象锁,也不能将object.wait()这样的语句写在当前上下文中。如: 

    复制代码
    package edu.sjtu.erplab.ObjectTest;
    
    class A
    {
        public synchronized void printThreadInfo() throws InterruptedException
        {
            Thread t=Thread.currentThread();
            System.out.println("ThreadID:"+t.getId()+", ThreadName:"+t.getName());
        }
    }
    
    
    
    public class ObjectWaitTest {
        public static void main(String args[])
        {
            A a=new A();
            //因为printThreadInfo()方法抛出InterruptedException异常,所以这里必须使用try-catch块
            try {
                a.printThreadInfo();
                a.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
    }
    复制代码

    程序运行会报错,运行结果如下:

    ThreadID:1, ThreadName:main
    Exception in thread "main" java.lang.IllegalMonitorStateException
        at java.lang.Object.wait(Native Method)
        at java.lang.Object.wait(Object.java:485)
        at edu.sjtu.erplab.ObjectTest.ObjectWaitTest.main(ObjectWaitTest.java:24)

    正确的写法应该是

    复制代码
    package edu.sjtu.erplab.ObjectTest;
    
    class A
    {
        public synchronized void printThreadInfo() throws InterruptedException
        {
            Thread t=Thread.currentThread();
            System.out.println("ThreadID:"+t.getId()+", ThreadName:"+t.getName());
    //        this.wait();//一直等待
            this.wait(1000);//等待1000ms
    //        super.wait(1000);
        }
    }
    
    
    
    public class ObjectWaitTest {
        public static void main(String args[])
        {
            A a=new A();
            //因为printThreadInfo()方法抛出InterruptedException异常,所以这里必须使用try-catch块
            try {
                a.printThreadInfo();
                //a.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            Thread t=Thread.currentThread();
            System.out.println("ThreadID:"+t.getId()+", ThreadName:"+t.getName());
        }
    }
    复制代码

    具体的可以参考多线程开发中提到的消费者与生产者案例的最后一个代码示例。

  • 相关阅读:
    css的em是根据什么来写的
    向一个对象数组里面添加新的属性 + 将一个对象数组数据拿出来变成另一个对象
    微信里iphone后退不刷新问题解决方案
    进到页面后input输入框自动获取焦点
    jquery checkbox反复调用attr('checked', true/false)只有第一次生效
    js promise中如何取到[[PromiseValue]]
    js 取得当天0点 / 23:59:59 时间
    jQuery获取包括当前元素的HTML
    C++ 实现 STL 标准库和算法(二)template 编程和迭代器粗解 实验楼笔记
    我现在怎么写博客笔记?
  • 原文地址:https://www.cnblogs.com/senior-engineer/p/4860252.html
Copyright © 2011-2022 走看看