Java wait和notify/notifyAll的使用方法

作者 : 开心源码 本文共1956个字,预计阅读时间需要5分钟 发布时间: 2022-05-13 共412人阅读

java的wait/notify的通知机制可以用来实现线程间通信。wait表示线程的等待,调用该方法会导致线程阻塞,直至另一线程调用notify或者notifyAll方法才唤醒它后继续执行。

注意事项

  1. wait和notify/notifyAll操作的对象需是synchronized锁持有对象。
  2. notify随机唤醒一个,notifyAll一律唤醒。
  3. 在锁持有对象调用wait进行等待时,会释放锁。
  4. lock.notifyAll重新获取锁之后,同样遵循synchronized同步执行。

1. 样例代码

import java.time.LocalTime;public class MainTest {    private static Object lock = new Object();    public static void main(String[] args) throws InterruptedException {        System.out.println("Main start " + LocalTime.now());        Thread thread = new Thread(new MyThread());        thread.start();        Thread thread2 = new Thread(new MyThread());        thread2.start();        System.out.println("Main sleep 5s " + LocalTime.now());        Thread.sleep(5000);        System.out.println("Main try to get lock... " + LocalTime.now());        synchronized(lock){            System.out.println("Main get lock and notifyAll " + LocalTime.now());            lock.notifyAll();        }        System.out.println("Main end");    }    public static class MyThread implements Runnable{        @Override        public void run() {            String threadName = Thread.currentThread().getName();            System.out.println(threadName + " run and try to get lock... " + LocalTime.now());            synchronized (lock){                System.out.println(threadName + " get lock and sleep 2s... " + LocalTime.now());                try {                    Thread.sleep(2000);                } catch (InterruptedException e) {                    e.printStackTrace();                }                System.out.println(threadName + " wait " + LocalTime.now());                try {                    lock.wait();                } catch (InterruptedException e) {                    e.printStackTrace();                }                System.out.println(threadName + " get lock agein and sleep 2s " + LocalTime.now());                try {                    Thread.sleep(2000);                } catch (InterruptedException e) {                    e.printStackTrace();                }            }            System.out.println(threadName + " end " + LocalTime.now());        }    }}

2. 输出结果

Main start 17:55:13.788Main sleep 5s 17:55:13.789Thread-0 run and try to get lock... 17:55:13.790Thread-1 run and try to get lock... 17:55:13.790Thread-0 get lock and sleep 2s... 17:55:13.790Thread-0 wait 17:55:15.791Thread-1 get lock and sleep 2s... 17:55:15.791Thread-1 wait 17:55:17.791Main try to get lock... 17:55:18.789Main get lock and notifyAll 17:55:18.789Main endThread-1 get lock agein and sleep 2s 17:55:18.789Thread-1 end 17:55:20.790Thread-0 get lock agein and sleep 2s 17:55:20.790Thread-0 end 17:55:22.791

3. 流程示用意

image.png

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » Java wait和notify/notifyAll的使用方法

发表回复