首页 技术 正文
技术 2022年11月15日
0 收藏 414 点赞 3,375 浏览 2623 个字

转载请注明出处:http://blog.csdn.net/ns_code/article/details/17229601

如果线程在等待时接到通知,但线程等待的条件还不满足,此时,线程接到的就是早期通知,如果条件满足的时间很短,但很快又改变了,而变得不再满足,这时也将发生早期通知。这种现象听起来很奇怪,下面通过一个示例程序来说明问题。

很简单,两个线程等待删除List中的元素,同时另外一个线程正要向其中添加项目。代码如下:

  1. import java.util.*;
  2. public class EarlyNotify extends Object {
  3. private List list;
  4. public EarlyNotify() {
  5. list = Collections.synchronizedList(new LinkedList());
  6. }
  7. public String removeItem() throws InterruptedException {
  8. print(“in removeItem() – entering”);
  9. synchronized ( list ) {
  10. if ( list.isEmpty() ) {  //这里用if语句会发生危险
  11. print(“in removeItem() – about to wait()”);
  12. list.wait();
  13. print(“in removeItem() – done with wait()”);
  14. }
  15. //删除元素
  16. String item = (String) list.remove(0);
  17. print(“in removeItem() – leaving”);
  18. return item;
  19. }
  20. }
  21. public void addItem(String item) {
  22. print(“in addItem() – entering”);
  23. synchronized ( list ) {
  24. //添加元素
  25. list.add(item);
  26. print(“in addItem() – just added: ‘” + item + “‘”);
  27. //添加后,通知所有线程
  28. list.notifyAll();
  29. print(“in addItem() – just notified”);
  30. }
  31. print(“in addItem() – leaving”);
  32. }
  33. private static void print(String msg) {
  34. String name = Thread.currentThread().getName();
  35. System.out.println(name + “: ” + msg);
  36. }
  37. public static void main(String[] args) {
  38. final EarlyNotify en = new EarlyNotify();
  39. Runnable runA = new Runnable() {
  40. public void run() {
  41. try {
  42. String item = en.removeItem();
  43. print(“in run() – returned: ‘” +
  44. item + “‘”);
  45. } catch ( InterruptedException ix ) {
  46. print(“interrupted!”);
  47. } catch ( Exception x ) {
  48. print(“threw an Exception!!!\n” + x);
  49. }
  50. }
  51. };
  52. Runnable runB = new Runnable() {
  53. public void run() {
  54. en.addItem(“Hello!”);
  55. }
  56. };
  57. try {
  58. //启动第一个删除元素的线程
  59. Thread threadA1 = new Thread(runA, “threadA1”);
  60. threadA1.start();
  61. Thread.sleep(500);
  62. //启动第二个删除元素的线程
  63. Thread threadA2 = new Thread(runA, “threadA2”);
  64. threadA2.start();
  65. Thread.sleep(500);
  66. //启动增加元素的线程
  67. Thread threadB = new Thread(runB, “threadB”);
  68. threadB.start();
  69. Thread.sleep(10000); // wait 10 seconds
  70. threadA1.interrupt();
  71. threadA2.interrupt();
  72. } catch ( InterruptedException x ) {}
  73. }
  74. }

执行结果如下:

分析:首先启动threadA1,threadA1在removeItem()中调用wait(),从而释放list上的对象锁。再过500ms,启动threadA2,threadA2调用removeItem(),获取list上的对象锁,也发现列表为空,从而在wait()方法处阻塞,释放list上的对象锁。再过500ms后,启动threadB,并调用addItem,获得list上的对象锁,并在list中添加一个元素,同时用notifyAll通知所有线程。

threadA1和threadA2都从wait()返回,等待获取list对象上的对象锁,并试图从列表中删除添加的元素,这就会产生麻烦,只有其中一个操作能成功。假设threadA1获取了list上的对象锁,并删除元素成功,在退出synchronized代码块时,它便会释放list上的对象锁,此时threadA2便会获取list上的对象锁,会继续删除list中的元素,但是list已经为空了,这便会抛出IndexOutOfBoundsException。

要避免以上问题只需将wait外围的if语句改为while循环即可,这样当list为空时,线程便会继续等待,而不会继续去执行删除list中元素的代码。

修改后的执行结果如下:

总结:在使用线程的等待/通知机制时,一般都要在while循环中调用wait()方法,满足条件时,才让while循环退出,这样一般也要配合使用一个boolean变量(或其他能判断真假的条件,如本文中的list.isEmpty()),满足while循环的条件时,进入while循环,执行wait()方法,不满足while循环的条件时,跳出循环,执行后面的代码。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295