Java中notify()和notifyAll()的使用区别

2025-05-29 0 43

notify() 和 notifyAll() 有什么区别?

先解释两个概念。

  • 等待池:假设一个线程A调用了某个对象的wait()方法,线程A就会释放该对象的锁后,进入到了该对象的等待池,等待池中的线程不会去竞争该对象的锁。
  • 锁池:只有获取了对象的锁,线程才能执行对象的 synchronized 代码,对象的锁每次只有一个线程可以获得,其他线程只能在锁池中等待

然后再来说notifynotifyAll的区别

  • 如果线程调用了对象的 wait()方法,那么线程便会处于该对象的等待池中,等待池中的线程不会去竞争该对象的锁。
  • 当有线程调用了对象的 notifyAll()方法(唤醒所有 wait 线程)或 notify()方法(只随机唤醒一个 wait 线程),被唤醒的的线程便会进入该对象的锁池中,锁池中的线程会去竞争该对象锁。也就是说,调用了notify后只要一个线程会由等待池进入锁池,而notifyAll会将该对象等待池内的所有线程移动到锁池中,等待锁竞争
  • 优先级高的线程竞争到对象锁的概率大,假若某线程没有竞争到该对象锁,它还会留在锁池中,唯有线程再次调用 wait()方法,它才会重新回到等待池中。而竞争到对象锁的线程则继续往下执行,直到执行完了 synchronized 代码块,它会释放掉该对象锁,这时锁池中的线程会继续竞争该对象锁。

综上,所谓唤醒线程,另一种解释可以说是将线程由等待池移动到锁池,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

到此这篇关于Javanotify()和notifyAll()的使用区别的文章就介绍到这了,更多相关Java notify()和notifyAll()内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!

原文链接:https://blog.csdn.net/meism5/article/details/90238268

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java中notify()和notifyAll()的使用区别 https://www.kuaiidc.com/104054.html

相关文章

发表评论
暂无评论