當一個線程進入wait之後,就必須等其他線程notify/notifyall,使用notifyall,可以喚醒
所有處於wait狀態的線程,使其重新進入鎖的爭奪隊列中,而notify只能喚醒一個.注意,任何時候只有一個線程可以獲得鎖,也就是說只有一個線程可以運行synchronized 中的代碼,notifyall只是讓處於wait的線程重新擁有鎖的爭奪權,但是只會有一個獲得鎖並執行.
那麼notify和notifyall在效果上又什麼實質區別呢?
主要的效果區別是notify用得不好容易導致死鎖,例如下面提到的例子.
public synchronized void put(Object o) {
while (buf.size()==MAX_SIZE) {
wait(); // called if the buffer is full (try/catch removed for brevity)
}
buf.add(o);
notify(); // called in case there are any getters or putters waiting
}
public synchronized Object get() {
// Y: this is where C2 tries to acquire the lock (i.e. at the beginning of the method)
while (buf.size()==0) {
wait(); // called if the buffer is empty (try/catch removed for brevity)
// X: this is where C1 tries to re-acquire the lock (see below)
}
Object o = buf.remove(0);
notify(); // called if there are any getters or putters waiting
return o;
}
除非你非常確定notify沒有問題,大部分情況還是是用notifyall.