notifyAll()用于唤醒所有等待特定对象监视器的线程,需在synchronized块中调用,配合wait()实现线程协作,如生产者-消费者模型中通过notifyAll()确保多个消费者或生产者被唤醒,避免线程阻塞。

在Java中,notifyAll() 用于唤醒所有正在等待特定对象监视器的线程。它通常与 synchronized 块、wait() 方法配合使用,实现线程间的协作。下面介绍如何正确使用 notifyAll() 来唤醒线程。
1. 理解 wait() 和 notifyAll() 的工作机制
这三个方法都定义在 Object 类中,必须在 synchronized 上下文中调用:
- wait():使当前线程释放锁并进入等待状态,直到被唤醒。
- notify():随机唤醒一个等待该对象锁的线程。
- notifyAll():唤醒所有等待该对象锁的线程,这些线程将重新竞争获取锁。
2. 使用步骤和代码示例
以下是一个生产者-消费者模型的简单示例,展示如何用 notifyAll() 正确唤醒多个等待线程:
import java.util.LinkedList;
import java.util.Queue;
public class NotifyAllExample {
private final Queue queue = new LinkedList<>();
private final int MAX_SIZE = 3;
public void produce(String item) throws InterruptedException {
synchronized (this) {
while (queue.size() == MAX_SIZE) {
System.out.println("队列已满,生产者等待...");
this.wait(); // 释放锁并等待
}
queue.add(item);
System.out.println("生产了: " + item);
this.notifyAll(); // 唤醒所有等待线程(包括消费者)
}
}
public String consume() throws InterruptedException {
synchronized (this) {
while (queue.isEmpty()) {
System.out.println("队列为空,消费者等待...");
this.wait(); // 释放锁并等待
}
String item = queue.poll();
System.out.println("消费了: " + item);
this.notifyAll(); // 唤醒所有等待线程(包括生产者)
return item;
}
}
} 3. 多线程测试 notifyAll 的效果
启动多个生产者和消费者线程,观察 notifyAll 如何唤醒多个等待线程:
Android文档-开发者指南-第一部分:入门-中英文对照版 Android提供了丰富的应用程序框架,它允许您在Java语言环境中构建移动设备的创新应用程序和游戏。在左侧导航中列出的文档提供了有关如何使用Android的各种API来构建应用程序的详细信息。第一部分:Introduction(入门) 0、Introduction to Android(引进到Android) 1、Application Fundamentals(应用程序基础) 2、Device Compatibility(设备兼容性) 3、
立即学习“Java免费学习笔记(深入)”;
public class TestNotifyAll {
public static void main(String[] args) {
NotifyAllExample example = new NotifyAllExample();
// 启动多个消费者线程
for (int i = 1; i <= 3; i++) {
new Thread(() -> {
try {
while (true) {
example.consume();
Thread.sleep(2000); // 模拟处理时间
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}, "Consumer-" + i).start();
}
// 启动生产者线程
new Thread(() -> {
int counter = 1;
try {
while (true) {
example.produce("item-" + counter++);
Thread.sleep(500);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}, "Producer").start();
}
}4. 关键注意事项
使用 notifyAll() 时需注意以下几点:
- 始终在 synchronized 块或方法中调用 wait() 和 notifyAll()。
- 使用 while 而不是 if 判断等待条件,防止虚假唤醒或条件变化后继续执行。
- notifyAll() 会唤醒所有等待线程,但它们需要重新竞争锁,只有一个能进入同步块。
- 适合场景:当多个线程可能等待同一条件时(如多个消费者),notifyAll() 更安全。
基本上就这些。notifyAll() 是实现线程协作的重要工具,合理使用可避免死锁和线程饥饿问题。









