Java中的notyfy()和notifyAll()的本质区别
作者:hao_kkkkk 时间:2022-06-05 22:46:19
wait()方法表示,放弃当前对资源的占有权,等啊等啊,一直等到有人通知我,我才会运行后面的代码。
notify()方法表示,当前的线程已经放弃对资源的占有,
通知等待的线程来获得对资源的占有权,但是只有一个线程能够从wait状态中恢复,
然后继续运行wait()后面的语句;
notifyAll()方法表示,当前的线程已经放弃对资源的占有,
通知所有的等待线程从wait()方法后的语句开始运行。
读出什么区别没有?
上例子,先是一个nofiyAll()的例子:
Java代码
package com.thread.wait;
public class Wait {
private int counter = 0;
private String name = null;
public Wait(int counter,String name){
this.counter = counter;
this.name = name;
}
public synchronized void doSomthing(){
int tempCounter = --counter;
if(tempCounter <= 0){
customizedNotifyAll();
}
else
{
while(tempCounter > 0){
try {
System.out.println(Thread.currentThread().getName()+"-<"+name+tempCounter+">"+"will invoke WAIT()");
--tempCounter;
wait();
} catch (InterruptedException e) {
e.printStackTrace();
notifyAll();
}
System.out.println(Thread.currentThread().getName()+"-<"+name+tempCounter+">"+"has been ACTIVED");
}
customizedNotifyAll();
}
}
public void customizedNotifyAll(){
notifyAll();
System.out.println(Thread.currentThread().getName()+"-<"+name+counter+">"+"::"+"INVOKED NOTIFYALL() AND FINISHED");
}
}
Java代码
package com.thread.wait;
public class TestThread implements Runnable {
private Wait wait;
public TestThread(Wait wait){
this.wait = wait;
}
public void run() {
wait.doSomthing();
}
public static void main(String [] args){
Wait wait = new Wait(4,"DAVID");
Thread t1 = new Thread(new TestThread(wait));
Thread t2 = new Thread(new TestThread(wait));
Thread t3 = new Thread(new TestThread(wait));
Thread t4 = new Thread(new TestThread(wait));
t1.start();
t2.start();
t3.start();
t4.start();
}
}
运行的结果:
Thread-0-<DAVID3>will invoke WAIT()
Thread-1-<DAVID2>will invoke WAIT()
Thread-2-<DAVID1>will invoke WAIT()
Thread-3-<DAVID0>::INVOKED NOTIFYALL() AND FINISHED
Thread-0-<DAVID2>has been ACTIVED
Thread-0-<DAVID2>will invoke WAIT()
Thread-1-<DAVID1>has been ACTIVED
Thread-1-<DAVID1>will invoke WAIT()
Thread-2-<DAVID0>has been ACTIVED
Thread-2-<DAVID0>::INVOKED NOTIFYALL() AND FINISHED
Thread-0-<DAVID1>has been ACTIVED
Thread-0-<DAVID1>will invoke WAIT()
Thread-1-<DAVID0>has been ACTIVED
Thread-1-<DAVID0>::INVOKED NOTIFYALL() AND FINISHED
Thread-0-<DAVID0>has been ACTIVED
Thread-0-<DAVID0>::INVOKED NOTIFYALL() AND FINISHED
看到了吧,一旦调用notifyAll()方法,所有的等待线程都会从调用wait()方法的地方继续运行起来。
这个运行结果可能每次都不一样,有时候只有两个线程运行完成而其余两个线程在等待其它线程调用notifyAll()方法,有时候只有三个线程运行完成,而另一个还在等待中。
由于本文是讲解notify以及notifyAll方法,所以对上面的原因不多加以解释。
然后是notify()方法的例子:
就是将wait类中的customizedNotifyAll()方法中的notifyAll()方法换成notify()方法
运行结果:
Thread-1-<DAVID3>will invoke WAIT()
Thread-0-<DAVID2>will invoke WAIT()
Thread-2-<DAVID1>will invoke WAIT()
Thread-3-<DAVID0>::INVOKED NOTIFYALL() AND FINISHED
Thread-1-<DAVID2>has been ACTIVED
Thread-1-<DAVID2>will invoke WAIT()
Did you see that?所有的等待线程中,只有一个线程运行完成了,而其它的线程还在傻傻地等待,poor guys!
每次运行的结果会不一样,但是始终只有一个线程能够运行完成。
Summary:
notify()方法只是让一个线程从wait中恢复过来,至于具体是哪个,那就得看那些线程的运气了(不设置优先级的情况下),继续执行后面的语句;
notifyAll()方法是让所有的线程从wait中恢复过来,继续执行后面的语句。
以上所述是小编给大家介绍的Java中的notyfy()和notifyAll()的本质区别网站的支持!
来源:http://blog.csdn.net/hao_kkkkk/article/details/54923844
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解决RestTemplate 请求接收自定义400+ 或500+错误
![](https://img.aspxhome.com/file/2023/6/85426_0s.png)
MyBatis的注解使用、ORM层优化方式(懒加载和缓存)
![](https://img.aspxhome.com/file/2023/8/86048_0s.jpg)
springboot项目启动慢的问题排查方式
![](https://img.aspxhome.com/file/2023/0/57590_0s.png)
详谈java中File类getPath()、getAbsolutePath()、getCanonical的区别
C#实现截图工具小项目
Java AWT中常用的三种布局管理器详解
![](https://img.aspxhome.com/file/2023/6/65586_0s.png)
逆波兰计算器(Java实现)
![](https://img.aspxhome.com/file/2023/4/85834_0s.jpg)
浅谈基于SpringBoot实现一个简单的权限控制注解
Java汉字转拼音工具类完整代码实例
Spring+Vue整合UEditor富文本实现图片附件上传的方法
![](https://img.aspxhome.com/file/2023/3/78033_0s.png)
C#使用Dispose模式实现手动对资源的释放
Mybatis中Mapper映射文件使用详解
![](https://img.aspxhome.com/file/2023/4/76244_0s.png)
浅谈MyBatis中@MapKey的妙用
![](https://img.aspxhome.com/file/2023/7/81887_0s.png)
DevExpress设置饼状图的Lable位置实例
![](https://img.aspxhome.com/file/2023/0/75620_0s.png)
spring-boot读取props和yml配置文件的方法
C#之继承实现
C#显示文件夹下所有图片文件的方法
IntelliJ IDEA安装目录和设置目录的说明(IntelliJ IDEA快速入门)
![](https://img.aspxhome.com/file/2023/2/86802_0s.jpg)