生产者消费者模式java
时间: 2023-10-12 10:05:15 浏览: 66
生产者消费者模式是一种常见的并发编程模式,用于解决多个线程之间的协作和通信问题。该模式包括两种角色:生产者和消费者。
生产者负责生产数据并将其放入缓冲区中,消费者则负责从缓冲区中取出数据并进行处理。在生产者和消费者之间使用一个共享的缓冲区来实现线程间通信。当缓冲区为空时,消费者等待,直到生产者向缓冲区中放入数据;当缓冲区为满时,生产者等待,直到消费者从缓冲区中取出数据。
Java中可以使用多种方式实现生产者消费者模式,以下是其中两种常见的实现方式:
1. 使用wait()和notify()方法
在Java中,线程之间的通信可以通过wait()和notify()方法实现。生产者和消费者可以共享一个对象,并在该对象上调用wait()和notify()方法来进行线程间通信。具体实现如下:
```
class Buffer {
private int data;
private boolean empty = true;
public synchronized int get() {
while (empty) {
try {
wait();
} catch (InterruptedException e) { }
}
empty = true;
notifyAll();
return data;
}
public synchronized void put(int data) {
while (!empty) {
try {
wait();
} catch (InterruptedException e) { }
}
empty = false;
this.data = data;
notifyAll();
}
}
class Producer implements Runnable {
private Buffer buffer;
public Producer(Buffer buffer) {
this.buffer = buffer;
}
public void run() {
for (int i = 0; i < 10; i++) {
buffer.put(i);
System.out.println("Produced " + i);
try {
Thread.sleep((int)(Math.random() * 100));
} catch (InterruptedException e) { }
}
}
}
class Consumer implements Runnable {
private Buffer buffer;
public Consumer(Buffer buffer) {
this.buffer = buffer;
}
public void run() {
for (int i = 0; i < 10; i++) {
int data = buffer.get();
System.out.println("Consumed " + data);
try {
Thread.sleep((int)(Math.random() * 100));
} catch (InterruptedException e) { }
}
}
}
public class Main {
public static void main(String[] args) {
Buffer buffer = new Buffer();
Producer producer = new Producer(buffer);
Consumer consumer = new Consumer(buffer);
new Thread(producer).start();
new Thread(consumer).start();
}
}
```
在上述代码中,Buffer类表示缓冲区,Producer类表示生产者,Consumer类表示消费者。生产者和消费者都是一个Runnable实例,可以通过new Thread(producer)和new Thread(consumer)来创建线程并启动。
2. 使用阻塞队列
Java中提供了阻塞队列(BlockingQueue)用于实现生产者消费者模式。阻塞队列是一种特殊的队列,当队列为空时,消费者线程会被阻塞;当队列为满时,生产者线程会被阻塞。
可以使用Java中的ArrayBlockingQueue或LinkedBlockingQueue来实现阻塞队列。具体实现如下:
```
import java.util.concurrent.ArrayBlockingQueue;
class Producer implements Runnable {
private ArrayBlockingQueue<Integer> buffer;
public Producer(ArrayBlockingQueue<Integer> buffer) {
this.buffer = buffer;
}
public void run() {
for (int i = 0; i < 10; i++) {
try {
buffer.put(i);
System.out.println("Produced " + i);
Thread.sleep((int)(Math.random() * 100));
} catch (InterruptedException e) { }
}
}
}
class Consumer implements Runnable {
private ArrayBlockingQueue<Integer> buffer;
public Consumer(ArrayBlockingQueue<Integer> buffer) {
this.buffer = buffer;
}
public void run() {
for (int i = 0; i < 10; i++) {
try {
int data = buffer.take();
System.out.println("Consumed " + data);
Thread.sleep((int)(Math.random() * 100));
} catch (InterruptedException e) { }
}
}
}
public class Main {
public static void main(String[] args) {
ArrayBlockingQueue<Integer> buffer = new ArrayBlockingQueue<>(10);
Producer producer = new Producer(buffer);
Consumer consumer = new Consumer(buffer);
new Thread(producer).start();
new Thread(consumer).start();
}
}
```
在上述代码中,Producer和Consumer类分别表示生产者和消费者,使用ArrayBlockingQueue作为缓冲区。生产者通过调用put()方法向队列中添加元素,消费者通过调用take()方法从队列中获取元素。当队列为空时,消费者线程会被阻塞;当队列为满时,生产者线程会被阻塞。
相关推荐
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)