1. 程式人生 > >轉:【Java並發編程】之十一:線程間通信中notify通知的遺漏(含代碼)

轉:【Java並發編程】之十一:線程間通信中notify通知的遺漏(含代碼)

key wait title net fill article 返回 0ms 註意

轉載請註明出處:http://blog.csdn.net/ns_code/article/details/17228213

notify通知的遺漏很容易理解,即threadA還沒開始wait的時候,threadB已經notify了,這樣,threadB通知是沒有任何響應的,當threadB退出synchronized代碼塊後,threadA再開始wait,便會一直阻塞等待,直到被別的線程打斷。

遺漏通知的代碼

下面給出一段代碼演示通知是如何遺漏的,如下:

[java] view plain copy
  1. public class MissedNotify extends Object {
  2. private Object proceedLock;
  3. public MissedNotify() {
  4. print("in MissedNotify()");
  5. proceedLock = new Object();
  6. }
  7. public void waitToProceed() throws InterruptedException {
  8. print("in waitToProceed() - entered");
  9. synchronized ( proceedLock ) {
  10. print("in waitToProceed() - about to wait()");
  11. proceedLock.wait();
  12. print("in waitToProceed() - back from wait()");
  13. }
  14. print("in waitToProceed() - leaving");
  15. }
  16. public void proceed() {
  17. print("in proceed() - entered");
  18. synchronized ( proceedLock ) {
  19. print("in proceed() - about to notifyAll()");
  20. proceedLock.notifyAll();
  21. print("in proceed() - back from notifyAll()");
  22. }
  23. print("in proceed() - leaving");
  24. }
  25. private static void print(String msg) {
  26. String name = Thread.currentThread().getName();
  27. System.out.println(name + ": " + msg);
  28. }
  29. public static void main(String[] args) {
  30. final MissedNotify mn = new MissedNotify();
  31. Runnable runA = new Runnable() {
  32. public void run() {
  33. try {
  34. //休眠1000ms,大於runB中的500ms,
  35. //是為了後調用waitToProceed,從而先notifyAll,後wait,
  36. //從而造成通知的遺漏
  37. Thread.sleep(1000);
  38. mn.waitToProceed();
  39. } catch ( InterruptedException x ) {
  40. x.printStackTrace();
  41. }
  42. }
  43. };
  44. Thread threadA = new Thread(runA, "threadA");
  45. threadA.start();
  46. Runnable runB = new Runnable() {
  47. public void run() {
  48. try {
  49. //休眠500ms,小於runA中的1000ms,
  50. //是為了先調用proceed,從而先notifyAll,後wait,
  51. //從而造成通知的遺漏
  52. Thread.sleep(500);
  53. mn.proceed();
  54. } catch ( InterruptedException x ) {
  55. x.printStackTrace();
  56. }
  57. }
  58. };
  59. Thread threadB = new Thread(runB, "threadB");
  60. threadB.start();
  61. try {
  62. Thread.sleep(10000);
  63. } catch ( InterruptedException x ) {}
  64. //試圖打斷wait阻塞
  65. print("about to invoke interrupt() on threadA");
  66. threadA.interrupt();
  67. }
  68. }


執行結果如下:

技術分享

分析:由於threadB在執行mn.proceed()之前只休眠了500ms,而threadA在執行mn.waitToProceed()之前休眠了1000ms,因此,threadB會先蘇醒,繼而執行mn.proceed(),獲取到proceedLock的對象鎖,繼而執行其中的notifyAll(),當退出proceed()方法中的synchronized代碼塊時,threadA才有機會獲取proceedLock的對象鎖,繼而執行其中的wait()方法,但此時notifyAll()方法已經執行完畢,threadA便漏掉了threadB的通知,便會阻塞下去。後面主線程休眠10秒後,嘗試中斷threadA線程,使其拋出InterruptedException。

修正後的代碼

為了修正MissedNotify,需要添加一個boolean指示變量,該變量只能在同步代碼塊內部訪問和修改。修改後的代碼如下:

[java] view plain copy
  1. public class MissedNotifyFix extends Object {
  2. private Object proceedLock;
  3. //該標誌位用來指示線程是否需要等待
  4. private boolean okToProceed;
  5. public MissedNotifyFix() {
  6. print("in MissedNotify()");
  7. proceedLock = new Object();
  8. //先設置為false
  9. okToProceed = false;
  10. }
  11. public void waitToProceed() throws InterruptedException {
  12. print("in waitToProceed() - entered");
  13. synchronized ( proceedLock ) {
  14. print("in waitToProceed() - entered sync block");
  15. //while循環判斷,這裏不用if的原因是為了防止早期通知
  16. while ( okToProceed == false ) {
  17. print("in waitToProceed() - about to wait()");
  18. proceedLock.wait();
  19. print("in waitToProceed() - back from wait()");
  20. }
  21. print("in waitToProceed() - leaving sync block");
  22. }
  23. print("in waitToProceed() - leaving");
  24. }
  25. public void proceed() {
  26. print("in proceed() - entered");
  27. synchronized ( proceedLock ) {
  28. print("in proceed() - entered sync block");
  29. //通知之前,將其設置為true,這樣即使出現通知遺漏的情況,也不會使線程在wait出阻塞
  30. okToProceed = true;
  31. print("in proceed() - changed okToProceed to true");
  32. proceedLock.notifyAll();
  33. print("in proceed() - just did notifyAll()");
  34. print("in proceed() - leaving sync block");
  35. }
  36. print("in proceed() - leaving");
  37. }
  38. private static void print(String msg) {
  39. String name = Thread.currentThread().getName();
  40. System.out.println(name + ": " + msg);
  41. }
  42. public static void main(String[] args) {
  43. final MissedNotifyFix mnf = new MissedNotifyFix();
  44. Runnable runA = new Runnable() {
  45. public void run() {
  46. try {
  47. //休眠1000ms,大於runB中的500ms,
  48. //是為了後調用waitToProceed,從而先notifyAll,後wait,
  49. Thread.sleep(1000);
  50. mnf.waitToProceed();
  51. } catch ( InterruptedException x ) {
  52. x.printStackTrace();
  53. }
  54. }
  55. };
  56. Thread threadA = new Thread(runA, "threadA");
  57. threadA.start();
  58. Runnable runB = new Runnable() {
  59. public void run() {
  60. try {
  61. //休眠500ms,小於runA中的1000ms,
  62. //是為了先調用proceed,從而先notifyAll,後wait,
  63. Thread.sleep(500);
  64. mnf.proceed();
  65. } catch ( InterruptedException x ) {
  66. x.printStackTrace();
  67. }
  68. }
  69. };
  70. Thread threadB = new Thread(runB, "threadB");
  71. threadB.start();
  72. try {
  73. Thread.sleep(10000);
  74. } catch ( InterruptedException x ) {}
  75. print("about to invoke interrupt() on threadA");
  76. threadA.interrupt();
  77. }
  78. }

執行結果如下:

技術分享

註意代碼中加了註釋的部分,在threadB進行通知之前,先將okToProceed置為true,這樣如果threadA將通知遺漏,那麽就不會進入while循環,也便不會執行wait方法,線程也就不會阻塞。如果通知沒有被遺漏,wait方法返回後,okToProceed已經被置為true,下次while循環判斷條件不成立,便會退出循環。

這樣,通過標誌位和wait、notifyAll的配合使用,便避免了通知遺漏而造成的阻塞問題。

總結:在使用線程的等待/通知機制時,一般都要配合一個boolean變量值(或者其他能夠判斷真假的條件),在notify之前改變該boolean變量的值,讓wait返回後能夠退出while循環(一般都要在wait方法外圍加一層while循環,以防止早期通知),或在通知被遺漏後,不會被阻塞在wait方法處。這樣便保證了程序的正確性。

轉:【Java並發編程】之十一:線程間通信中notify通知的遺漏(含代碼)