生产者消费者模式通过共享缓冲区协调生产与消费速度,Java中可用wait/notify或BlockingQueue实现。前者需手动同步控制,后者由并发包自动处理阻塞与线程安全,更简洁高效。

生产者消费者模式是多线程编程中常见的设计模式,用于解决生产数据和消费数据速度不匹配的问题。在Java中,可以通过多种方式实现该模式,核心目标是保证线程安全、避免资源竞争,并实现线程间的有效通信。
定义一个共享缓冲区(例如队列),生产者线程向其中添加数据,消费者线程从中取出数据。当缓冲区满时,生产者等待;当缓冲区空时,消费者等待。
示例代码:
class Buffer {
private int data;
private boolean available = false;
public synchronized void put(int value) {
while (available) {
try {
wait(); // 缓冲区已占用,生产者等待
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
data = value;
available = true;
notifyAll(); // 唤醒消费者
}
public synchronized int take() {
while (!available) {
try {
wait(); // 缓冲区为空,消费者等待
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
available = false;
notifyAll(); // 唤醒生产者
return data;
}
}
创建生产者和消费者线程分别调用 put() 和 take() 方法即可。
BlockingQueue 的 put() 方法在队列满时自动阻塞,take() 方法在队列空时自动阻塞,无需手动管理锁和等待通知。
立即学习“Java免费学习笔记(深入)”;
示例代码:
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ArrayBlockingQueue;
class Producer implements Runnable {
private final BlockingQueue<Integer> queue;
public Producer(BlockingQueue<Integer> queue) {
this.queue = queue;
}
public void run() {
try {
for (int i = 0; i < 10; i++) {
queue.put(i);
System.out.println("生产者生产: " + i);
Thread.sleep(100); // 模拟生产耗时
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
class Consumer implements Runnable {
private final BlockingQueue<Integer> queue;
public Consumer(BlockingQueue<Integer> queue) {
this.queue = queue;
}
public void run() {
try {
while (true) {
Integer value = queue.take();
System.out.println("消费者消费: " + value);
Thread.sleep(200); // 模拟消费耗时
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
主程序中启动多个生产者和消费者线程即可:
public class ProducerConsumerDemo {
public static void main(String[] args) {
BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(5);
Thread producer = new Thread(new Producer(queue));
Thread consumer = new Thread(new Consumer(queue));
producer.start();
consumer.start();
}
}
你可以为“非满”和“非空”分别创建不同的 Condition,从而只唤醒特定类型的线程,提升效率。
关键点:这种方式适合复杂场景,但日常开发中推荐优先使用 BlockingQueue。
基本上就这些。BlockingQueue 是最简洁高效的实现方式,适合绝大多数情况。wait/notify 和 Lock/Condition 更适合学习原理或定制需求。以上就是如何在Java中实现生产者消费者模式的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号