- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Java中notify()和notifyAll()的使用区别由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
notify() 和 notifyAll() 有什么区别?
综上,所谓唤醒线程,另一种解释可以说是将线程由等待池移动到锁池,notifyAll调用后,会将全部线程由等待池移到锁池,然后参与锁的竞争,竞争成功则继续执行,如果不成功则留在锁池等待锁被释放后再次参与竞争。而notify只会唤醒一个线程.
有了这些理论基础,后面的notify可能会导致死锁,而notifyAll则不会的例子也就好解释了 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
public
class
TestNotifyNotifyAll {
private
static
Object obj =
new
Object();
public
static
void
main(String[] args) {
//测试 RunnableImplA wait()
Thread t1 =
new
Thread(
new
RunnableImplA(obj));
Thread t2 =
new
Thread(
new
RunnableImplA(obj));
t1.start();
t2.start();
//RunnableImplB notify()
Thread t3 =
new
Thread(
new
RunnableImplB(obj));
t3.start();
// //RunnableImplC notifyAll()
// Thread t4 = new Thread(new RunnableImplC(obj));
// t4.start();
}
}
class
RunnableImplA
implements
Runnable {
private
Object obj;
public
RunnableImplA(Object obj) {
this
.obj = obj;
}
public
void
run() {
System.out.println(
"run on RunnableImplA"
);
synchronized
(obj) {
System.out.println(
"obj to wait on RunnableImplA"
);
try
{
obj.wait();
}
catch
(InterruptedException e) {
e.printStackTrace();
}
System.out.println(
"obj continue to run on RunnableImplA"
);
}
}
}
class
RunnableImplB
implements
Runnable {
private
Object obj;
public
RunnableImplB(Object obj) {
this
.obj = obj;
}
public
void
run() {
System.out.println(
"run on RunnableImplB"
);
System.out.println(
"睡眠3秒..."
);
try
{
Thread.sleep(
3000
);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
synchronized
(obj) {
System.out.println(
"notify obj on RunnableImplB"
);
obj.notify();
}
}
}
class
RunnableImplC
implements
Runnable {
private
Object obj;
public
RunnableImplC(Object obj) {
this
.obj = obj;
}
public
void
run() {
System.out.println(
"run on RunnableImplC"
);
System.out.println(
"睡眠3秒..."
);
try
{
Thread.sleep(
3000
);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
synchronized
(obj) {
System.out.println(
"notifyAll obj on RunnableImplC"
);
obj.notifyAll();
}
}
}
|
结果:仅调用一次 obj.notify(),线程 t1 或 t2 中的一个始终在等待被唤醒,程序不终止 。
run on RunnableImplA obj to wait on RunnableImplA run on RunnableImplA obj to wait on RunnableImplA run on RunnableImplB 睡眠3秒... notify obj on RunnableImplB obj continue to run on RunnableImplA 。
把 t3 注掉,启动 t4 线程。调用 obj.notifyAll() 方法 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public
class
TestNotifyNotifyAll {
private
static
Object obj =
new
Object();
public
static
void
main(String[] args) {
//测试 RunnableImplA wait()
Thread t1 =
new
Thread(
new
RunnableImplA(obj));
Thread t2 =
new
Thread(
new
RunnableImplA(obj));
t1.start();
t2.start();
// //RunnableImplB notify()
// Thread t3 = new Thread(new RunnableImplB(obj));
// t3.start();
//RunnableImplC notifyAll()
Thread t4 =
new
Thread(
new
RunnableImplC(obj));
t4.start();
}
}
|
结果:t1、t2线程均可以执行完毕 。
run on RunnableImplA obj to wait on RunnableImplA run on RunnableImplA obj to wait on RunnableImplA run on RunnableImplC 睡眠3秒... notifyAll obj on RunnableImplC obj continue to run on RunnableImplA obj continue to run on RunnableImplA 。
到此这篇关于Java中notify()和notifyAll()的使用区别的文章就介绍到这了,更多相关Java notify()和notifyAll()内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。
原文链接:https://blog.csdn.net/meism5/article/details/90238268 。
最后此篇关于Java中notify()和notifyAll()的使用区别的文章就讲到这里了,如果你想了解更多关于Java中notify()和notifyAll()的使用区别的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在尝试在我安装的 Laravel 5.1 中设置 Gulp。我已经运行然后命令 npm install正如 Laravel 文档中所指定的那样,效果很好。 但是,当我现在运行命令时 gulp我收到
我有两个扩展线程的类和一个等待/通知 class A extends Thread { int r = 20; public void run() { try {
我正在尝试在后台服务中启动通知,这也是位置监听器。我有一个功能: public Notification CreateNotification(){ Intent notificati
编辑:我将其范围缩小到 python 代码中 Notifier.notify('Something') 的问题。当 python 脚本从 launchd 启动时,这不会产生预期的行为。我的其他 pyt
我正在尝试使用 bootstrap-notify v3.1.3、typescript、aurelia 和 VS2015 在我的网站上显示警报。 我的 aurelia 组件是: //myAlert.ts
注:other question的标题不同,导致无法识别为匹配的。 系统.类 TCollection = class(TPersistent) protected procedure Notify
我正在将一个项目从 Sprockets 迁移到 Webpacker。 我似乎无法正确运行的最后一件事是通知。 我曾经能够做到:$.notify('Test') 但现在我得到了 Uncaught Typ
我在一个项目中有多个用户控件,其中一个从 XML 中检索项目,创建“ClassItem”类型的对象,并应通知其他用户控件有关这些项目的信息。 我为我的对象创建了一个类(所有项目都将具有的“模型”):
我以为我会理解 Java 中的并发概念,但现在有一件事打破了我的理解: 为什么我必须在同步块(synchronized block)中包含对 wait() 和 notify() 方法的调用? 假设我有
我正在开发 NodeJS/Electron/Angular 应用程序,我正在使用 node-notifier 模块。一切正常,但在我的通知消息底部有一个不合适的“toast”字符串。它仅在我使用“图标
我正在基于Sproutcore 1.9.1的Web应用程序上工作。要从服务器检索数据 发出一个SC.Request.getUrl()请求,该请求在除IE8之外的所有浏览器中都可以正常工作。 对于IE8
我试图写一个关于如何使用wait()和notify()的示例,但是似乎无法通知wait() public class Transfer { private int[] data; pr
我想使用QAudioInput从麦克风捕获声音,对其进行处理然后再播放。据我了解,我需要连接到notify信号和内部处理程序,以便用户使用readAll()函数来获取原始数据。但是问题是,此处理函数永
public class Signal2NoiseRatio { public ImagePlus SingleSNR(ImagePlus imagePlus) throws Interrup
为什么只有第一个任务退出后才调用dispatchGroup.notify? 下面的代码输出如下: 1) Did the other thing **2) Did all the things** 3)
假设线程 T1 正在等待进入同步块(synchronized block),线程 T2 在同步块(synchronized block)内 wait(),并且线程 T3 在同步块(synchroniz
我正在尝试仅使用等待/通知同步来实现合并排序。我知道更高级的结构,例如 Fork/Join、Executors。等等,但我需要在这里使用工作/通知。基于此https://courses.cs.wash
了解 notifyAll 让我对 notify 产生了一些疑问:在典型情况下,我们有多个线程正在等待对 methody notify 的调用。当这种情况发生时,其中一个线程(之前调用了 wait 方法
我正在使用 wait() 和 notify() 编写一个示例程序,但是当调用 notify() 时,会出现多个线程被唤醒而不是一个。 代码是: public class MyQueue { O
根据17.2.4. Interactions of Waits, Notification, and Interruption : Similarly, notifications cannot be
我是一名优秀的程序员,十分优秀!