(See http://www.suneca.com/article.asp?id=56)
1)Object 類定義了 wait()、notify() 和 notifyAll() 方法。可以讓線程相互通知事件的發生。要執行這些方法,必須擁有相關對象的鎖。
2)wait() 會讓調用線程休眠,直到用 Thread.interrupt() 中斷它、過了指定的時間、或者另一個線程用 notify() 或 notifyAll() 喚醒它。
3)當對某個對象調用 notify() 時,如果有任何線程正在通過 wait() 等待該對象,那麼就會喚醒其中一個線程。當對某個對象調用 notifyAll() 時,會喚醒所有正在等待該對象的線程。
假如我們有兩條線程,我們希望線程的輸出順序是:
線程t1輸出十次
線程t2輸出十次
線程t1輸出十次
線程t2輸出十次
那程序該如何實現?
我們的設計思路是:
使用鎖的機制,首先,線程1進入可運行狀態後
1)線程t1獲取對象的鎖
2)線程t1完成輸出十個數的任務
3)喚醒其他(t2)正在等待的線程
4)自身阻塞,釋放鎖。
5)線程t2獲取鎖
6)線程t2完成輸出十個數的任務
7)喚醒其他(t1)正在等待的線程
8)自身阻塞,釋放鎖。
9)。。。。
程序的實現如下:
/**
* 主程序. 使用wait(),notify(),notifyAll()方法
*
* @author <a href='http://www.suneca.com'>ZIZZ</a>
*
* @Create-Time:2008-4-21 上午12:51:59
*/
public class ThreadLock {
public static void main(String[] args) {
//共享線程實例的線程.
ShareRunnable share = new ShareRunnable();
Thread t1 = new Thread(share, "t1");
Thread t2 = new Thread(share, "t2");
t1.start();
t2.start();
}
}
/**
*
* @author <a href='http://www.suneca.com'>ZIZZ</a>
*
* @Create-Time:2008 上午12:14:26
*/
class ShareRunnable implements Runnable {
public void run() {
//第一個線程運行時,對共享的線程對象進行加鎖
synchronized (this) {
for (int i = 1; i <= 100; i++) {
//輸出當前的值
System.out.println(Thread.currentThread().getName() + " : " + i);
// 如果i爲10的倍數
if (i % 10 == 0) {
try {
// 喚起其他線程
notifyAll();
// 當前正在運行的線程阻塞,釋放擁有該對象的鎖.
if(i == 100)break;
else wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
運行結果片斷:
假如在開發的時候,並沒有寫上synchronized (this) {...}的同步語句,那將會出現一些問題,因爲使用wait()、notify(),它需要有owner,假如沒有寫上這個同步語句塊,那系統將會執出如下錯誤信息:
at java.lang.Object.notifyAll(Native Method)
at zizz.ShareRunnable.run(ThreadLock.java:41)
at java.lang.Thread.run(Thread.java:595)