天天看點

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;

                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();

                //啟動計算線程

                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

運作結果表明,程式中有異常,并且多次運作結果可能有多種輸出結果。這就是說明,這個多線程的互動程式還存在問題。究竟是出了什麼問題,需要深入的分析和思考,下面将做具體分析。

實際上,上面這個代碼中,我們期望的是讀取結果的線程在計算線程調用notifyAll()之前等待即可。 但是,如果計算線程先執行,并在讀取結果線程等待之前調用了notify()方法,那麼又會發生什麼呢?這種情況是可能發生的。因為無法保證線程的不同部分将按照什麼順序來執行。幸運的是當讀取線程運作時,它隻能馬上進入等待狀态----它沒有做任何事情來檢查等待的事件是否已經發生。  ----是以,如果計算線程已經調用了notifyAll()方法,那麼它就不會再次調用notifyAll(),----并且等待的讀取線程将永遠保持等待。這當然是開發者所不願意看到的問題。

是以,當等待的事件發生時,需要能夠檢查notifyAll()通知事件是否已經發生。

通常,解決上面問題的最佳方式是将

繼續閱讀