java:线程等待与唤醒 - Object的wait()和notify()

java:线程等待与唤醒 - Object的wait()和notify()

1 前言

java使用Object类的wait()和notify()方法,可以实现线程等待和唤醒(Object类为所有类的父类,即所有类天然具有线程等待和唤醒的方法,一般使用Object类的wait()和notify()方法即可)。

使用wait()、notify()时,有一些注意点,比如wait和notify方法需在同步代码块(synchronized 代码块)或方法(方法包含锁实例对象的synchronized实例方法,以及锁类对象的synchronized static方法)中使用,一般成对出现。

在一般执行时,还具有先后顺序的强依赖,即先执行wait,再执行notify,若先执行notify,再执行wait,后续无线程唤醒操作时,阻塞的线程将无法被唤醒。

2 使用

使用前,先关注下java文档中的说明:

* This method should only be called by a thread that is the owner
* of this object's monitor. A thread becomes the owner of the
* object's monitor in one of three ways:
* <ul>
* <li>By executing a synchronized instance method of that object.
* <li>By executing the body of a {@code synchronized} statement
*     that synchronizes on the object.
* <li>For objects of type {@code Class,} by executing a
*     synchronized static method of that class.
* </ul>

* Only one thread at a time can own an object's monitor.
*
* @throws  IllegalMonitorStateException  if the current thread is not
*               the owner of this object's monitor.
* @see        java.lang.Object#notifyAll()
* @see        java.lang.Object#wait()
public final native void notify();


* This method should only be called by a thread that is the owner
* of this object's monitor. See the {@code notify} method for a
* description of the ways in which a thread can become the owner of
* a monitor.
*
* @throws  IllegalMonitorStateException  if the current thread is not
*               the owner of the object's monitor.
* @throws  InterruptedException if any thread interrupted the
*             current thread before or while the current thread
*             was waiting for a notification.  The <i>interrupted
*             status</i> of the current thread is cleared when
*             this exception is thrown.
* @see        java.lang.Object#notify()
* @see        java.lang.Object#notifyAll()
*/
public final void wait() throws InterruptedException {
   wait(0);
}

上述表明,notify执行时,若当前线程不是Object的monitor的持有者,则抛出IllegalMonitorStateException。

判断是否当前线程持有Object的monitor,即上述提到的3个方式,要么在synchronized代码块中执行object.wait()或object.notify(),要么就是synchronized实例方法,此时synchronized锁对象为实例对象,若是synchronized static 方法,则synchronized锁对象为类对象,注意不同方式使用不同的锁对象即可,此3种方式表示当前线程为Object的monitor持有者,可以顺利执行wait()或notify()方法,反之则抛出IllegalMonitorStateException。

常见的使用方式如下:

public class TestObjectWaitAndNotify {

    private Object lockObj = new Object();

    private void doSubmit(){
        System.out.println("成功提交!");
    }

    public void run() throws Exception{
        AtomicBoolean f = new AtomicBoolean(false);

        new Thread(() -> {
            synchronized (this.lockObj){
                System.out.println(Thread.currentThread().getName() + "开始执行");
                while(!f.get()){
                    try {
                        this.lockObj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(Thread.currentThread().getName() + "执行结束");
                doSubmit();
            }
        }, "xiaoxu1").start();

        TimeUnit.SECONDS.sleep(2);

        new Thread(() -> {
            synchronized (this.lockObj){
                System.out.println(Thread.currentThread().getName() + "开始执行");
                f.set(true);
                this.lockObj.notify();
                System.out.println(Thread.currentThread().getName() + "执行结束");
            }
        }, "xiaoxu2").start();

    }

    public static void main(String[] args) throws Exception{
        TestObjectWaitAndNotify n = new TestObjectWaitAndNotify();
        n.run();
    }
}

执行结果如下:

xiaoxu1开始执行
xiaoxu2开始执行
xiaoxu2执行结束
xiaoxu1执行结束
成功提交!

上述执行时,因为对象的监视器monitor含有该对象的锁、阻塞队列和同步队列,执行lockObj.wait()时,除了使当前线程进入阻塞队列,还需要释放锁(否则后续执行synchronized (this.lockObj)的第二个线程无法获取到锁并执行唤醒操作),然后后续休眠2秒后,第二个线程执行synchronized (this.lockObj),再次获取到锁(锁需是同一个对象的monitor持有的锁),执行唤醒操作,即阻塞队列中的休眠线程被再次唤醒并执行,执行后续的doSubmit(),且因为标志位改变,故而不再发起wait,整个过程结束。

另Object.notifyAll()方法,即唤醒当前阻塞队列的全部休眠线程(唤醒操作意即,notify、notifyAll会将阻塞队列中的线程,放入同步队列中等待执行,同时竞争锁失败的线程,也会放入同步队列中等待同步执行)。

注意,上述线程的等待和唤醒操作,因为加锁(synchronized决定了第一个线程会先于第二个线程获取到锁)和代码顺序,故而是先等待,再唤醒;若代码顺序为先唤醒,再等待,即便操作都加上synchronized(加上synchronized,保证了wait先执行,notify后执行),但因为代码操作顺序改变,阻塞队列没有休眠线程,此时再次执行wait,线程1将被一直阻塞无法唤醒。故wait()和notify()具有先后执行顺序的强依赖关系。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值