生产者消费者问题用java并且生产者类和消费者类都继承 Runnable类,并设定生产者消费者等待时间并给出注释
时间: 2024-05-01 14:23:17 浏览: 21
以下是一个简单的 Java 生产者消费者问题的实现,其中生产者类和消费者类都继承 Runnable 类。在本例中,我们使用了一个共享的缓冲区(即一个列表),生产者向缓冲区中添加元素,消费者从缓冲区中取出元素。如果缓冲区已满,则生产者必须等待,而如果缓冲区为空,则消费者必须等待。
```java
import java.util.ArrayList;
import java.util.List;
public class ProducerConsumerExample {
public static void main(String[] args) {
List<Integer> buffer = new ArrayList<>();
int maxSize = 5;
Thread producerThread = new Thread(new Producer(buffer, maxSize, 1000), "Producer");
Thread consumerThread = new Thread(new Consumer(buffer, 1000), "Consumer");
producerThread.start();
consumerThread.start();
}
static class Producer implements Runnable {
private List<Integer> buffer;
private int maxSize;
private int waitTime;
public Producer(List<Integer> buffer, int maxSize, int waitTime) {
this.buffer = buffer;
this.maxSize = maxSize;
this.waitTime = waitTime;
}
public void run() {
for (int i = 0; i < 10; i++) {
synchronized (buffer) {
while (buffer.size() == maxSize) {
try {
System.out.println("Buffer is full, waiting...");
buffer.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
buffer.add(i);
System.out.println("Produced " + i);
buffer.notifyAll();
}
try {
Thread.sleep(waitTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
static class Consumer implements Runnable {
private List<Integer> buffer;
private int waitTime;
public Consumer(List<Integer> buffer, int waitTime) {
this.buffer = buffer;
this.waitTime = waitTime;
}
public void run() {
while (true) {
synchronized (buffer) {
while (buffer.isEmpty()) {
try {
System.out.println("Buffer is empty, waiting...");
buffer.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
int value = buffer.remove(0);
System.out.println("Consumed " + value);
buffer.notifyAll();
}
try {
Thread.sleep(waitTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
```
在上面的代码中,我们创建了一个生产者和一个消费者线程,并将它们启动。生产者每隔一定时间向缓冲区中添加一个元素,消费者每隔一定时间从缓冲区中取出一个元素。在生产者和消费者的 run() 方法中,我们使用 synchronized 和 wait() / notifyAll() 方法来实现等待和通知机制。
注释已经在代码中给出,希望对您有所帮助!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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_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)