Java線程:線程的交互
SCJP5學習筆記
線程交互是比較複雜的問題,SCJP要求不很基礎:給定一個場景,編寫代碼來恰當使用等待、通知和通知全部線程。
1、線程交互的基礎知識
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()並不意味着這時該鎖變得可用。
2、多個線程在等待一個對象鎖時候使用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()通知事件是否已經發生。
一般,解決上面問題的最佳方式是將