Java中Object对象wait/notify/notifyAll方法详细解析

简书:capo 转载请注明原创出处,谢谢!

前言:

今天,我们讲讲Object中wait和notify/notifyAll这一组方法,我们来看看JDK中关于这两个方法的说明:

/**    
       引起当前线程等待直到另一个线程调用当前对象的notify方法或notify()方法或者一些其他的线程中断当前线程,或者一个指定的时间已经过去
     * Causes the current thread to wait until another thread invokes the
     * {@link java.lang.Object#notify()} method or the
     * {@link java.lang.Object#notifyAll()} method for this object, or
     * some other thread interrupts the current thread, or a certain
     * amount of real time has elapsed.
     * <p>
     
     * This method is similar to the {@code wait} method of one
     * argument, but it allows finer control over the amount of time to
     * wait for a notification before giving up. The amount of real time,
     * measured in nanoseconds, is given by:
     * <blockquote>
     * <pre>
     * 1000000*timeout+nanos</pre></blockquote>
     * <p>
     * In all other respects, this method does the same thing as the
     * method {@link #wait(long)} of one argument. In particular,
     * {@code wait(0, 0)} means the same thing as {@code wait(0)}.
     * <p>
     * The current thread must own this object's monitor. The thread
     * releases ownership of this monitor and waits until either of the
     * following two conditions has occurred:
     * <ul>
     * <li>Another thread notifies threads waiting on this object's monitor
     *     to wake up either through a call to the {@code notify} method
     *     or the {@code notifyAll} method.
     * <li>The timeout period, specified by {@code timeout}
     *     milliseconds plus {@code nanos} nanoseconds arguments, has
     *     elapsed.
     * </ul>
     * <p>
     * The thread then waits until it can re-obtain ownership of the
     * monitor and resumes execution.
     * <p>
     * As in the one argument version, interrupts and spurious wakeups are
     * possible, and this method should always be used in a loop:
     * <pre>
     *     synchronized (obj) {
     *         while (<condition does not hold>)
     *             obj.wait(timeout, nanos);
     *         ... // Perform action appropriate to condition
     *     }
     * </pre>
     * 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.
     *
     * @param      timeout   the maximum time to wait in milliseconds.
     * @param      nanos      additional time, in nanoseconds range
     *                       0-999999.
     * @throws  IllegalArgumentException      if the value of timeout is
     *                      negative or the value of nanos is
     *                      not in the range 0-999999.
     * @throws  IllegalMonitorStateException  if the current thread is not
     *               the owner of this 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.
     */
    public final void wait(long timeout, int nanos) throws InterruptedException {
        if (timeout < 0) {
            throw new IllegalArgumentException("timeout value is negative");
        }

        if (nanos < 0 || nanos > 999999) {
            throw new IllegalArgumentException(
                                "nanosecond timeout value out of range");
        }

        if (nanos > 0) {
            timeout++;
        }

        wait(timeout);
    }


 /**
     * Wakes up a single thread that is waiting on this object's
     * monitor. If any threads are waiting on this object, one of them
     * is chosen to be awakened. The choice is arbitrary and occurs at
     * the discretion of the implementation. A thread waits on an object's
     * monitor by calling one of the {@code wait} methods.
     * <p>
     * The awakened thread will not be able to proceed until the current
     * thread relinquishes the lock on this object. The awakened thread will
     * compete in the usual manner with any other threads that might be
     * actively competing to synchronize on this object; for example, the
     * awakened thread enjoys no reliable privilege or disadvantage in being
     * the next thread to lock this object.
     * <p>
     * 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>
     * <p>
     * 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();

我总结了一下关于这个方法使用注意事项:

  • 引起当前线程等待,直到另一个线程调用此对象的notify()方法或notifyAll()方法.或者指定线程超时等待一定时间后。
  • 这个超时时间单位是纳秒,其计算公式为: 1000000*timeout+nanos
  • 如果使用wait(0)和wait(0,0)是等价的
  • 如果当前对象在没有获得锁的监视器的情况下就调用wait或者notify/notifyAll方法就是抛出IllegalMonitorStateException异常
  • 当前对象的wait方法会暂时释放掉对象监视器的锁,所以wait必须是在synchronized同步块中使用,因为synchronized同步块进入是默认是要获取对象监视器的。同理notify/notifyAll操作也要在对象获取监视器的情况下去唤醒一个等待池中的线程
  • wait操作还要在一个循环中使用,防止虚假唤醒

wait/notify在工作中的应用,等待通知机制(消费者-生产者模式)

一个线程修改了一个对象的值,而另一个线程感知道了变化,然后进行相应的操作,整个过程开始于一个线程,而最终执行又是另一个线程。前者是生产者,后者是消费者。接下来我们使用wait/notify实现这个机制

package com.minglangx.object;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
* 

  * @ClassName: WaitNotify

  * @Description: 使用wait/notify实现等待通知机制

  * @author minglangx

  * @date 2017年9月4日 下午4:16:30

  *


  
*/
public class WaitNotify {
 
 public static boolean flag = true;
 public static Object lock = new Object();
 
 
 
 public static void main(String[] args){
     
     Thread waitTHread = new Thread(new Wait(),"WaitThread");
     waitTHread.start();
     try {
         Thread.sleep(1);
     } catch (InterruptedException e) {
         e.printStackTrace();
     }
     
     Thread notifyThread = new Thread(new Notify(),"NotifyThread");
     notifyThread.start();
     
 }
 
 
 static class Wait implements Runnable{
     
     @Override
     public void run() {
         
         //获取 lock对象监视器 并加锁
         synchronized (lock) {
             //当条件不满足时,继续wait,同时只是暂时释放了lock对象上的锁,并将当前对象防止到对象的等待队列中
             while(flag) {
                 try {
                     
                     System.out.println(Thread.currentThread() 
                             + "flag is true. wait@ " 
                             + new SimpleDateFormat("HH:mm:ss")
                             .format(new Date()));
                     
                     lock.wait();
                     
                     
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }
                 
                 
             }
             
             //当条件满足时,完成工作
             System.out.println(Thread.currentThread() 
                     + "flag is true. wait@ " 
                     + new SimpleDateFormat("HH:mm:ss")
                     .format(new Date()));
             
             
         }
         
         
     }
     
     
     
 }
 
 static class Notify implements Runnable{
     
     @Override
     public void run() {
         /*
          * 获取对象的监视器
          */
         synchronized (lock) {
             //获取对象上的锁,然后通知等待队列中的所有对象,但这个时候不会释放锁
             System.out.println(Thread.currentThread()
                      + " 持有锁..notify @" 
                      + new SimpleDateFormat("HH:mm:ss").format(new Date()));
             
             //调用该方法后,将会把所有等待队列中的线程全部移动到同步队列中
             lock.notifyAll();
             //将条件置为 false
             flag = false;
             try {
                 Thread.sleep(5);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
             
             
             
         }
         
         
         //再次加锁
         synchronized (lock) {
             System.out.println(Thread.currentThread()
                      + " 再次持有锁..sleep @" 
                      + new SimpleDateFormat("HH:mm:ss").format(new Date()));
             
             try {
                 Thread.sleep(5);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
             
         }
         
     }
     
     
 }
 

}

这段代码最后输出:

image.png

我们看到当调用notify并没有释放掉对象上的锁,而是要等待synchronized代码块走完在释放掉对象上的锁

这段代码向我们说明了几点

  • 调用wait()方法后,线程状态由 running 变为等待wait, 并将当前线程放入等待队列
  • notify、notifyAll方法调用后,等待线程依旧不会从wait()返回,需要调用notify()或者notifyAll()的线程释放掉锁后,等待线程才有机会从wait()返回
  • notify()方法是将等待队列中一个等待线程从等待队列移动到同步队列中,而notifyAll则是将所有等待队列中的线程移动到同步队列中,被移动的线程状态由 running变为 阻塞blocked

为此我们规范一下这个等待、通知机制(消费者,生产者模式)如何编写
等待者(消费者)
编写代码步骤:

  1. 获取对象上的锁
  2. 如果条件不满足,则调用对象上的wait()方法,应该使用一个while()条件判断
  3. 条件满足则执行对应的业务逻辑
    其中伪代码:
    synchronized(对象) {
    while(条件不满足){
    对象.wait();
    }
    处理对应的业务逻辑
    }

通知者(生产者)
编写代码步骤:
1) 获取对象上的锁

  1. 改变条件
  2. 通知所有(一个)等待在对象上的线程
    对应的伪代码:
    synchronized(对象) {
    改变条件
    对象.notifyAll();
    }

总结:

  • 使用wait或者notify()方法一定要在同步代码块中使用,而wait一般要在while循环中使用
  • wait/notify可以实现生产者消费者模式,其原理是调用wait时将线程放入等待队列,而调用notify时将等待队列中的线程移动到同步队列
  • wait/notify机制是成对出现的,它们的实现依赖于锁的同步机制
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,390评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,821评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,632评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,170评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,033评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,098评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,511评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,204评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,479评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,572评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,341评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,893评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,171评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,486评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,676评论 2 335

推荐阅读更多精彩内容