1 wait,notify,notifyAll方法详解
1.1 作用、用法
我们可以通过上面方法控制一些线程去休息或唤醒
当一个线程使用wait方法时,这个线程被阻塞(阻塞阶段)并且释放锁
由阻塞状态变为唤醒阶段有几种情况?
- 另一个线程调用这个对象的notify()方法且刚好被唤醒的是本线程
- 另一个线程调用这个对象的notifyAll()方法
- 过了wait( long timeout )规定的时间,如果传入0就是永久等待
- 线程自身调用了interrupt()
遇到中断会由阻塞状态变为唤醒状态
public static void main (String[] args) throws InterruptedException {Thread t1 = new Thread(new Runnable() {@Overridepublic void run () {try {System.out.println("t1 begin sleep 2000 seconds");Thread.sleep(2000000);System.out.println("t1 awaking");} catch (InterruptedException e) {System.out.println("t1 is interrupted while sleeping");return;}System.out.println("t1 continue working");}});t1.start();Thread.sleep(1000);//打断子线程的休眠,让子线程从sleep函数返回t1.interrupt();//等待子线程执行完毕t1.join();System.out.println("main thread is over");
}
输出
1.2 代码演示
wait演示
public class Wait {static Object object = new Object();static class Thread1 extends Thread{@Overridepublic void run () {synchronized (object){System.out.println("线程"+Thread.currentThread().getName()+"开始执行");try {object.wait();//释放了锁,并且将线程t1阻塞} catch (InterruptedException e) {e.printStackTrace();}System.out.println("线程"+Thread.currentThread().getName()+"释放了锁");}}}static class Thread2 extends Thread{@Overridepublic void run () {synchronized (object){object.notify();System.out.println("线程"+Thread.currentThread().getName()+"调用了notify()");}}}public static void main (String[] args) throws InterruptedException {Thread1 t1 = new Thread1();Thread2 t2 = new Thread2();t1.start();Thread.sleep(2000);t2.start();}
}
输出
线程Thread-0开始执行
线程Thread-1调用了notify()
线程Thread-0释放了锁
描述一下代码执行的整个过程:
- 线程t1得到了锁object,打印出"线程Thread-0开始执行"
- 线程执行到了object.wait()。锁object被释放,线程t1被阻塞
- 线程t2得到了锁object,执行语句object.notify(),此时线程t1被
- 唤醒,但是该线程不能获得锁object。必须要等t2释放了锁之后才能运行
- t1重新获取到锁之后会返回之前的位置,继续执行代码
notifyAll演示
/*** 线程1和2首先被阻塞,线程3唤醒它们*/
public class WaitNotifyAll implements Runnable{private static final Object resourceA = new Object();@Overridepublic void run () {synchronized (resourceA){System.out.println(Thread.currentThread().getName()+"got resourceA lock.");try {System.out.println(Thread.currentThread().getName()+"waits to start.");resourceA.wait();System.out.println(Thread.currentThread().getName()+"got resourceA lock too.");} catch (InterruptedException e) {e.printStackTrace();}}}public static void main (String[] args) throws InterruptedException {WaitNotifyAll r = new WaitNotifyAll();Thread t1 = new Thread(r);Thread t2 = new Thread(r);Thread t3 = new Thread(new Runnable() {@Overridepublic void run () {synchronized (resourceA) {resourceA.notifyAll();System.out.println("ThreadC notified.");}}});t1.start();t2.start();Thread.sleep(4000);t3.start();}
}
输出
Thread-0got resourceA lock.
Thread-0waits to start.
Thread-1got resourceA lock.
Thread-1waits to start.
ThreadC notified.
Thread-0got resourceA lock too.
Thread-1got resourceA lock too.
notify演示
public class WaitNotifyAll implements Runnable{private static final Object resourceA = new Object();@Overridepublic void run () {synchronized (resourceA){System.out.println(Thread.currentThread().getName()+"got resourceA lock.");try {System.out.println(Thread.currentThread().getName()+"waits to start.");resourceA.wait();System.out.println(Thread.currentThread().getName()+"got resourceA lock too.");} catch (InterruptedException e) {e.printStackTrace();}}}public static void main (String[] args) throws InterruptedException {WaitNotifyAll r = new WaitNotifyAll();Thread t1 = new Thread(r);Thread t2 = new Thread(r);Thread t3 = new Thread(new Runnable() {@Overridepublic void run () {synchronized (resourceA) {resourceA.notify();System.out.println("ThreadC notified.");}}});t1.start();t2.start();Thread.sleep(4000);t3.start();}
}
输出
证明wait只会释放当前这把锁monitor
/*** 证明wait只释放当前的那把锁*/
public class WaitNotifyReleaseOwnMonitor {private static Object resourceA = new Object();private static Object resourceB = new Object();public static void main (String[] args) {Thread thread1 = new Thread(new Runnable() {@Overridepublic void run () {synchronized (resourceA) {System.out.println("ThreadA got resourceA lock.");synchronized (resourceB) {System.out.println("ThreadA got resourceB lock.");try {System.out.println("ThreadA releases resourceA lock.");resourceA.wait();} catch (InterruptedException e) {e.printStackTrace();}}}}});Thread thread2 = new Thread(new Runnable() {@Overridepublic void run () {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}synchronized (resourceA) {System.out.println("ThreadB got resourceA lock.");System.out.println("ThreadB try got resourceA lock.");synchronized (resourceB) {System.out.println("ThreadB got resourceB lock.");}}}});thread1.start();thread2.start();}
}
输出
1.3 特点、性质
- 执行到 wait , notify , notifyAll 时,我们必须获取到monitor(三个方法的使用大多数是在synchronized代码块中)
- notify方法只会唤醒一个线程,这个被唤醒的线程不是由我们决定的。而是由JVM决定的
- 以上三个方法都是Object类的方法,而且都是native
- 类似功能的Condition
- 同时持有多个锁的情况:上面代码thread1同时持有resourceA和resourceB两把锁。wait只能释放当前锁。所以很容易发生死锁
1.4 wait原理
要明白途中数字代表的情况
现在我们来补充之前线程六大状态转移图
2 生产者消费者—使用wait_notify实现
问题引入:现实生活中生产者消费者之间的速度可能不匹配。其中有可能生产者比较快,也有可能生产者比较慢,可能会出现等待现象。这个时候出现了一个设计模式——生产者消费者模式很好的解决了等待问题。该模式实际上将生产者和消费者进行解耦,提高了两者之间的默契度。该模式通常利用一个容器(通常是阻塞队列)来解决两者之间的耦合问题。两者之间的通信通过容器完成
通信大致如下:
代码演示
/*** 生产者消费者模式:wait/notify*/
public class ProducerConsumerModel {public static void main (String[] args) {EventStorage eventStorage = new EventStorage();Producer producer = new Producer(eventStorage);Consumer consumer = new Consumer(eventStorage);Thread pro = new Thread(producer);Thread con = new Thread(consumer);pro.start();con.start();}
}class Producer implements Runnable{private EventStorage storage;public Producer (EventStorage storage) {this.storage = storage;}@Overridepublic void run () {for (int i = 0; i < 100; i++) {storage.put();}}
}
class Consumer implements Runnable{private EventStorage storage;public Consumer (EventStorage storage) {this.storage = storage;}@Overridepublic void run () {for (int i = 0; i < 100; i++) {storage.take();}}
}class EventStorage{private int maxSize;private LinkedList<Date> storage;public EventStorage () {}{this.maxSize = 10;this.storage = new LinkedList<>();}/*** 放入* synchronized修饰默认方法:锁对象默认是this*/public synchronized void put(){while (storage.size() == maxSize){//如果满了等待try {wait();} catch (InterruptedException e) {e.printStackTrace();}}storage.add(new Date());System.out.println("仓库里有了"+storage.size()+"个产品");notify();//唤醒沉睡的消费者}/*** 拿出*/public synchronized void take(){while(storage.isEmpty()){try {wait();} catch (InterruptedException e) {e.printStackTrace();}}System.out.println("拿到了"+storage.poll()+",现在仓库还剩下 "+storage.size());notify();//通知生产者干活了}
}
输出
仓库里有了1个产品
仓库里有了2个产品
仓库里有了3个产品
仓库里有了4个产品
仓库里有了5个产品
仓库里有了6个产品
仓库里有了7个产品
仓库里有了8个产品
仓库里有了9个产品
仓库里有了10个产品
拿到了Fri May 10 14:42:34 CST 2024,现在仓库还剩下 9
拿到了Fri May 10 14:42:34 CST 2024,现在仓库还剩下 8
拿到了Fri May 10 14:42:34 CST 2024,现在仓库还剩下 7
拿到了Fri May 10 14:42:34 CST 2024,现在仓库还剩下 6
拿到了Fri May 10 14:42:34 CST 2024,现在仓库还剩下 5
拿到了Fri May 10 14:42:34 CST 2024,现在仓库还剩下 4
拿到了Fri May 10 14:42:34 CST 2024,现在仓库还剩下 3
拿到了Fri May 10 14:42:34 CST 2024,现在仓库还剩下 2
拿到了Fri May 10 14:42:34 CST 2024,现在仓库还剩下 1
拿到了Fri May 10 14:42:34 CST 2024,现在仓库还剩下 0
...
仓库里有了1个产品
仓库里有了2个产品
拿到了Fri May 10 14:42:34 CST 2024,现在仓库还剩下 1
拿到了Fri May 10 14:42:34 CST 2024,现在仓库还剩下 0
仓库里有了1个产品
仓库里有了2个产品
仓库里有了3个产品
仓库里有了4个产品
仓库里有了5个产品
仓库里有了6个产品
仓库里有了7个产品
仓库里有了8个产品
仓库里有了9个产品
仓库里有了10个产品
3 面试题
写代码:实现两个线程交替打印0~100的奇偶数
描述:一个线程打印奇数,一个线程打印偶数。它们之间交替打印
思路:使用关键字synchronized
/*** 实现两个线程交替打印0~100的奇偶数* 使用synchronized*/
public class WaitNotifyPrintEvenSyn {private static int i = 0;private static Object lock = new Object();public static void main (String[] args) {new Thread(new Runnable() {@Overridepublic void run () {while (i < 100) {synchronized (lock) {if ((i & 1) == 0) {System.out.println(Thread.currentThread().getName() + " 打印 " + i);i++;}}}}}).start();new Thread(new Runnable() {@Overridepublic void run () {while (i < 100) {synchronized (lock) {if ((i & 1) == 1) {System.out.println(Thread.currentThread().getName() + " 打印 " + i);i++;}}}}}).start();}
}
思路:使用wait/notify(更好)
/*** 使用wait/notify* 1. 拿到锁,我们打印* 2. 唤醒别人,休眠自己*/
public class WaitNotifyPrintOddEveWait {private static int i = 0;private static Object lock = new Object();static class TurnningThread implements Runnable{@Overridepublic void run () {while(i<100){synchronized (lock){System.out.println(Thread.currentThread().getName()+":"+i++);lock.notify();try {lock.wait();} catch (InterruptedException e) {e.printStackTrace();}}}}}public static void main (String[] args) {TurnningThread turnningThread = new TurnningThread();new Thread(turnningThread,"x").start();new Thread(turnningThread,"y").start();}
}
注意:上面的代码的逻辑一定要在脑子里面想好
手写生产者消费者设计模式
这个前面有
为什么wait()需要在同步代码块内使用,而sleep()不需要
答案:主要是为了让通信变得可靠,防止死锁等待的发生。如果我们没有将wait/notify/notifyAll放在同步代码块中,没了同步代码块的保护,就会发生一种现象:在线程快要执行到了wait()
时,线程突然被切换。切换到线程2上面,执行了线程2的notify()
。这样就在wait()
之前执行了notify()
。这样不符合我们设计wait/notify的初衷(wait方法尽量要在notify方法之前执行)。所以设计者考虑上面的问题,将需要多个线程之间的配合的动作(方法)在同步代码块中执行。但是sleep()
针对一个线程的方法,所以不必要在同步代码块中
为什么线程通信的方法wait(), notify(), notifyAll()被定义在Object类中?而sleep定义在Thread类中
答案:在Java规定中wait(), notify(), notifyAll()是锁级别的操作,而锁是属于对象的而不是线程中。如果将wait(), notify(), notifyAll()变为线程级别的操作,因为一个线程可能有多个锁,我们没有办法实现上面这样的灵活的逻辑了
wait方法属于Object对象的,那调用Thread.wait会怎样?
答案:因为wait是锁级别的,这样调用就表示Thread是一个锁。Thread还是一个特殊的存在:当线程退出的时候会自动地执行notify( JVM源码中有)。所以我们调用wait方法或常见锁对象的时候不要使用Thread类
如何选择notify和notifyAll?
唤醒一个线程和唤醒多个问题
调用notifyAll之后所有的线程全部被唤醒,它们会一起抢夺锁,如果某线程抢夺失败怎么办?
失败者会等待重新释放锁,然后再次抢
用suspend()和resume()来阻塞线程可以吗?为什么?
不推荐,推荐使用wait和notify来实现相关功能
悟空老师课程笔记
如有不对请在评论区指正