Java線程:線程的交互

Java線程:線程的交互
 
SCJP5學習筆記
 
線程交互是比較複雜的問題,SCJP要求不很基礎:給定一個場景,編寫代碼來恰當使用等待、通知和通知所有線程。
 
一、線程交互的基礎知識
 
SCJP所要求的線程交互知識點需要從java.lang.Object的類的三個方法來學習:
 
 void notify() 
          喚醒在此對象監視器上等待的單個線程。 
 void notifyAll() 
          喚醒在此對象監視器上等待的所有線程。 
 void wait() 
          導致當前的線程等待,直到其他線程調用此對象的 notify() 方法或 notifyAll() 方法。
 
當然,wait()還有另外兩個重載方法:
 void wait(long timeout) 
          導致當前的線程等待,直到其他線程調用此對象的 notify() 方法或 notifyAll() 方法,或者超過指定的時間量。 
 void wait(long timeout, int nanos) 
          導致當前的線程等待,直到其他線程調用此對象的 notify() 方法或 notifyAll() 方法,或者其他某個線程中斷當前線程,或者已超過某個實際時間量。
 
以上這些方法是幫助線程傳遞線程關心的時間狀態。
 
關於等待/通知,要記住的關鍵點是:
必須從同步環境內調用wait()、notify()、notifyAll()方法。線程不能調用對象上等待或通知的方法,除非它擁有那個對象的鎖。
wait()、notify()、notifyAll()都是Object的實例方法。與每個對象具有鎖一樣,每個對象可以有一個線程列表,他們等待來自該信號(通知)。線程通過執行對象上的wait()方法獲得這個等待列表。從那時候起,它不再執行任何其他指令,直到調用對象的notify()方法爲止。如果多個線程在同一個對象上等待,則將只選擇一個線程(不保證以何種順序)繼續執行。如果沒有線程等待,則不採取任何特殊操作。
 
下面看個例子就明白了:
/** 
* 計算輸出其他線程鎖計算的數據 

* @author leizhimin 2008-9-15 13:20:38 
*/
 
public class ThreadA { 
    public static void main(String[] args) { 
        ThreadB b = new ThreadB(); 
        //啓動計算線程 
        b.start(); 
        //線程A擁有b對象上的鎖。線程爲了調用wait()或notify()方法,該線程必須是那個對象鎖的擁有者 
        synchronized (b) { 
            try { 
                System.out.println("等待對象b完成計算。。。"); 
                //當前線程A等待 
                b.wait(); 
            } catch (InterruptedException e) { 
                e.printStackTrace(); 
            } 
            System.out.println("b對象計算的總和是:" + b.total); 
        } 
    } 
}
 
/** 
* 計算1+2+3 ... +100的和 

* @author leizhimin 2008-9-15 13:20:49 
*/
 
public class ThreadB extends Thread { 
    int total; 

    public void run() { 
        synchronized (this) { 
            for (int i = 0; i < 101; i++) { 
                total += i; 
            } 
            //(完成計算了)喚醒在此對象監視器上等待的單個線程,在本例中線程A被喚醒 
            notify(); 
        } 
    } 
}
 
等待對象b完成計算。。。 
b對象計算的總和是:5050 

Process finished with exit code 0 
 
千萬注意:
當在對象上調用wait()方法時,執行該代碼的線程立即放棄它在對象上的鎖。然而調用notify()時,並不意味着這時線程會放棄其鎖。如果線程榮然在完成同步代碼,則線程在移出之前不會放棄鎖。因此,只要調用notify()並不意味着這時該鎖變得可用。
 
二、多個線程在等待一個對象鎖時候使用notifyAll()
 
在多數情況下,最好通知等待某個對象的所有線程。如果這樣做,可以在對象上使用notifyAll()讓所有在此對象上等待的線程衝出等待區,返回到可運行狀態。
 
下面給個例子:
/** 
* 計算線程 

* @author leizhimin 2008-9-20 11:15:46 
*/
 
public class Calculator extends Thread { 
        int total; 

        public void run() { 
                synchronized (this) { 
                        for (int i = 0; i < 101; i++) { 
                                total += i; 
                        } 
                } 
                //通知所有在此對象上等待的線程 
                notifyAll(); 
        } 
}
 
/** 
* 獲取計算結果並輸出 

* @author leizhimin 2008-9-20 11:15:22 
*/
 
public class ReaderResult extends Thread { 
        Calculator c; 

        public ReaderResult(Calculator c) { 
                this.c = c; 
        } 

        public void run() { 
                synchronized (c) { 
                        try { 
                                System.out.println(Thread.currentThread() + "等待計算結果。。。"); 
                                c.wait(); 
                        } catch (InterruptedException e) { 
                                e.printStackTrace(); 
                        } 
                        System.out.println(Thread.currentThread() + "計算結果爲:" + c.total); 
                } 
        } 

        public static void main(String[] args) { 
                Calculator calculator = new Calculator(); 

                //啓動三個線程,分別獲取計算結果 
                new ReaderResult(calculator).start(); 
                new ReaderResult(calculator).start(); 
                new ReaderResult(calculator).start(); 
                //啓動計算線程 
                calculator.start(); 
        } 
}
 
運行結果:
Thread[Thread-1,5,main]等待計算結果。。。 
Thread[Thread-2,5,main]等待計算結果。。。 
Thread[Thread-3,5,main]等待計算結果。。。 
Exception in thread "Thread-0" java.lang.IllegalMonitorStateException: current thread not owner 
  at java.lang.Object.notifyAll(Native Method) 
  at threadtest.Calculator.run(Calculator.java:18) 
Thread[Thread-1,5,main]計算結果爲:5050 
Thread[Thread-2,5,main]計算結果爲:5050 
Thread[Thread-3,5,main]計算結果爲:5050 

Process finished with exit code 0 
 
運行結果表明,程序中有異常,並且多次運行結果可能有多種輸出結果。這就是說明,這個多線程的交互程序還存在問題。究竟是出了什麼問題,需要深入的分析和思考,下面將做具體分析。
 
實際上,上面這個代碼中,我們期望的是讀取結果的線程在計算線程調用notifyAll()之前等待即可。 但是,如果計算線程先執行,並在讀取結果線程等待之前調用了notify()方法,那麼又會發生什麼呢?這種情況是可能發生的。因爲無法保證線程的不同部分將按照什麼順序來執行。幸運的是當讀取線程運行時,它只能馬上進入等待狀態----它沒有做任何事情來檢查等待的事件是否已經發生。  ----因此,如果計算線程已經調用了notifyAll()方法,那麼它就不會再次調用notifyAll(),----並且等待的讀取線程將永遠保持等待。這當然是開發者所不願意看到的問題。
 
因此,當等待的事件發生時,需要能夠檢查notifyAll()通知事件是否已經發生。
 
通常,解決上面問題的最佳方式是將
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章