wait()、notify()、notifyAll() 是 Object 基類方法,而非 Thread 方法。每個對象天然擁有監視器鎖,這三個方法正是操作該鎖的核心 API。
synchronized(obj) 塊內調用,即持有對象鎖的線程才能調用notify/notifyAll 喚醒或中斷;喚醒後需重新獲取鎖才能繼續wait() 應放在 while(condition) 循環中,防止無條件喚醒
public class Container {
private final List<Object> list = new ArrayList<>();
private final int maxSize;
public Container(int maxSize) { this.maxSize = maxSize; }
public synchronized void produce(Object item) throws InterruptedException {
while (list.size() >= maxSize) wait(); // 滿了等待
list.add(item);
notifyAll(); // 喚醒消費者
}
public synchronized Object consume() throws InterruptedException {
while (list.isEmpty()) wait(); // 空了等待
Object item = list.remove(0);
notifyAll(); // 喚醒生產者
return item;
}
}
class Producer implements Runnable {
private final Container container;
private int count = 0;
Producer(Container c) { this.container = c; }
@Override
public void run() {
try {
while (true) {
container.produce(new Object());
System.out.println("生產第 " + (++count) + " 個");
Thread.sleep(500); // 模擬生產耗時
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
class Consumer implements Runnable {
private final Container container;
private int count = 0;
Consumer(Container c) { this.container = c; }
@Override
public void run() {
try {
while (true) {
container.consume();
System.out.println("消費第 " + (++count) + " 個");
Thread.sleep(1000); // 模擬消費耗時
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
public class Main {
public static void main(String[] args) {
Container container = new Container(5); // 容量 5
new Thread(new Producer(container), "生產者").start();
new Thread(new Consumer(container), "消費者").start();
}
}
wait/notify 封裝在容器內部,業務代碼無感知,降低出錯概率InterruptedException 並恢復中斷狀態,支持優雅關閉
BlockingQueue<Object> queue = new ArrayBlockingQueue<>(5);
// 生產者:queue.put(item) // 滿阻塞
// 消費者:queue.take() // 空阻塞
JDK 並發包提供開箱即用的線程安全隊列,內部已正確實現等待/通知機制,生產環境強烈建議直接使用,避免手寫 wait/notify 的易錯細節。
wait/notify/notifyAll 是基於對象監視器的底層協作原語,核心在於「條件檢查 + 釋放鎖等待 + 狀態改變後通知」。掌握其本質有助於理解高級併發組件原理,但實際開發請優先選用 BlockingQueue、Semaphore、CountDownLatch 等高級工具。