轉:【Java並發編程】之十一:線程間通信中notify通知的遺漏(含代碼)
轉載請註明出處:http://blog.csdn.net/ns_code/article/details/17228213
notify通知的遺漏很容易理解,即threadA還沒開始wait的時候,threadB已經notify了,這樣,threadB通知是沒有任何響應的,當threadB退出synchronized代碼塊後,threadA再開始wait,便會一直阻塞等待,直到被別的線程打斷。
遺漏通知的代碼
下面給出一段代碼演示通知是如何遺漏的,如下:
[java] view plain copy- public class MissedNotify extends Object {
- private Object proceedLock;
- public MissedNotify() {
- print("in MissedNotify()");
- proceedLock = new Object();
- }
- public void waitToProceed() throws InterruptedException {
- print("in waitToProceed() - entered");
- synchronized ( proceedLock ) {
- print("in waitToProceed() - about to wait()");
- proceedLock.wait();
- print("in waitToProceed() - back from wait()");
- }
- print("in waitToProceed() - leaving");
- }
- public void proceed() {
- print("in proceed() - entered");
- synchronized ( proceedLock ) {
- print("in proceed() - about to notifyAll()");
- proceedLock.notifyAll();
- print("in proceed() - back from notifyAll()");
- }
- print("in proceed() - leaving");
- }
- private static void print(String msg) {
- String name = Thread.currentThread().getName();
- System.out.println(name + ": " + msg);
- }
- public static void main(String[] args) {
- final MissedNotify mn = new MissedNotify();
- Runnable runA = new Runnable() {
- public void run() {
- try {
- //休眠1000ms,大於runB中的500ms,
- //是為了後調用waitToProceed,從而先notifyAll,後wait,
- //從而造成通知的遺漏
- Thread.sleep(1000);
- mn.waitToProceed();
- } catch ( InterruptedException x ) {
- x.printStackTrace();
- }
- }
- };
- Thread threadA = new Thread(runA, "threadA");
- threadA.start();
- Runnable runB = new Runnable() {
- public void run() {
- try {
- //休眠500ms,小於runA中的1000ms,
- //是為了先調用proceed,從而先notifyAll,後wait,
- //從而造成通知的遺漏
- Thread.sleep(500);
- mn.proceed();
- } catch ( InterruptedException x ) {
- x.printStackTrace();
- }
- }
- };
- Thread threadB = new Thread(runB, "threadB");
- threadB.start();
- try {
- Thread.sleep(10000);
- } catch ( InterruptedException x ) {}
- //試圖打斷wait阻塞
- print("about to invoke interrupt() on threadA");
- threadA.interrupt();
- }
- }
執行結果如下:
分析:由於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- public class MissedNotifyFix extends Object {
- private Object proceedLock;
- //該標誌位用來指示線程是否需要等待
- private boolean okToProceed;
- public MissedNotifyFix() {
- print("in MissedNotify()");
- proceedLock = new Object();
- //先設置為false
- okToProceed = false;
- }
- public void waitToProceed() throws InterruptedException {
- print("in waitToProceed() - entered");
- synchronized ( proceedLock ) {
- print("in waitToProceed() - entered sync block");
- //while循環判斷,這裏不用if的原因是為了防止早期通知
- while ( okToProceed == false ) {
- print("in waitToProceed() - about to wait()");
- proceedLock.wait();
- print("in waitToProceed() - back from wait()");
- }
- print("in waitToProceed() - leaving sync block");
- }
- print("in waitToProceed() - leaving");
- }
- public void proceed() {
- print("in proceed() - entered");
- synchronized ( proceedLock ) {
- print("in proceed() - entered sync block");
- //通知之前,將其設置為true,這樣即使出現通知遺漏的情況,也不會使線程在wait出阻塞
- okToProceed = true;
- print("in proceed() - changed okToProceed to true");
- proceedLock.notifyAll();
- print("in proceed() - just did notifyAll()");
- print("in proceed() - leaving sync block");
- }
- print("in proceed() - leaving");
- }
- private static void print(String msg) {
- String name = Thread.currentThread().getName();
- System.out.println(name + ": " + msg);
- }
- public static void main(String[] args) {
- final MissedNotifyFix mnf = new MissedNotifyFix();
- Runnable runA = new Runnable() {
- public void run() {
- try {
- //休眠1000ms,大於runB中的500ms,
- //是為了後調用waitToProceed,從而先notifyAll,後wait,
- Thread.sleep(1000);
- mnf.waitToProceed();
- } catch ( InterruptedException x ) {
- x.printStackTrace();
- }
- }
- };
- Thread threadA = new Thread(runA, "threadA");
- threadA.start();
- Runnable runB = new Runnable() {
- public void run() {
- try {
- //休眠500ms,小於runA中的1000ms,
- //是為了先調用proceed,從而先notifyAll,後wait,
- Thread.sleep(500);
- mnf.proceed();
- } catch ( InterruptedException x ) {
- x.printStackTrace();
- }
- }
- };
- Thread threadB = new Thread(runB, "threadB");
- threadB.start();
- try {
- Thread.sleep(10000);
- } catch ( InterruptedException x ) {}
- print("about to invoke interrupt() on threadA");
- threadA.interrupt();
- }
- }
執行結果如下:
註意代碼中加了註釋的部分,在threadB進行通知之前,先將okToProceed置為true,這樣如果threadA將通知遺漏,那麽就不會進入while循環,也便不會執行wait方法,線程也就不會阻塞。如果通知沒有被遺漏,wait方法返回後,okToProceed已經被置為true,下次while循環判斷條件不成立,便會退出循環。
這樣,通過標誌位和wait、notifyAll的配合使用,便避免了通知遺漏而造成的阻塞問題。
總結:在使用線程的等待/通知機制時,一般都要配合一個boolean變量值(或者其他能夠判斷真假的條件),在notify之前改變該boolean變量的值,讓wait返回後能夠退出while循環(一般都要在wait方法外圍加一層while循環,以防止早期通知),或在通知被遺漏後,不會被阻塞在wait方法處。這樣便保證了程序的正確性。
轉:【Java並發編程】之十一:線程間通信中notify通知的遺漏(含代碼)