设计产品类、生产者类和消费者类,生成相应类的对象,生产者和消费者同时对同一产品(多个)进行生产或消费,利用线程平衡生产和消费的过程。共享缓冲区最多可以放置4个产品,生产者A和生产者B各生产多个产品;消费者1、2、3每次可以取一个产品。,输出生产者、消费者的状态。
时间: 2024-11-25 20:18:44 浏览: 16
在这个场景中,我们可以设计一个简单的生产者-消费者模型,其中包含三个主要类:Product、Producer 和 Consumer。这里是一个简化的Java实现示例:
```java
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
class Product {
private String name;
private boolean available;
public Product(String name) {
this.name = name;
this.available = true;
}
public String getName() {
return name;
}
public synchronized boolean isAvailable() {
return available;
}
public synchronized void setAvailable(boolean available) {
this.available = available;
}
}
class Buffer {
private Queue<Product> buffer;
private ReentrantLock lock;
private Condition notFull;
private Condition notEmpty;
public Buffer(int capacity) {
buffer = new LinkedList<>();
lock = new ReentrantLock();
notFull = lock.newCondition();
notEmpty = lock.newCondition();
for (int i = 0; i < capacity; i++) {
Product product = new Product("Product" + i);
buffer.offer(product);
}
}
public synchronized void put(Product product) throws InterruptedException {
while (buffer.size() == buffer.capacity()) {
notFull.await();
}
buffer.offer(product);
notEmpty.signal();
}
public synchronized Product take() throws InterruptedException {
while (buffer.isEmpty()) {
notEmpty.await();
}
Product product = buffer.poll();
if (product != null) {
product.setAvailable(false);
notFull.signal();
}
return product;
}
}
class Producer extends Thread {
private Buffer buffer;
private int id;
public Producer(Buffer buffer, int id) {
this.buffer = buffer;
this.id = id;
}
@Override
public void run() {
try {
for (int i = 0; i < 10; i++) {
Product product = new Product("Product from producer " + id + "-" + i);
buffer.put(product);
System.out.printf("Producer %d produced %s%n", id, product.getName());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Consumer extends Thread {
private Buffer buffer;
public Consumer(Buffer buffer) {
this.buffer = buffer;
}
@Override
public void run() {
try {
for (int i = 0; i < 3; i++) {
Product product = buffer.take();
if (product != null) {
System.out.printf("Consumer %d consumed %s%n", i + 1, product.getName());
product.setAvailable(true);
} else {
break; // 如果缓冲区为空,跳出循环
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Main {
public static void main(String[] args) {
Buffer buffer = new Buffer(4);
Producer producerA = new Producer(buffer, 0);
Producer producerB = new Producer(buffer, 1);
Consumer consumer1 = new Consumer(buffer);
Consumer consumer2 = new Consumer(buffer);
Consumer consumer3 = new Consumer(buffer);
producerA.start();
producerB.start();
consumer1.start();
consumer2.start();
consumer3.start();
producerA.join();
producerB.join();
consumer1.join();
consumer2.join();
consumer3.join();
}
}
```
这个例子中,生产者通过put方法将产品放入缓冲区,消费者则通过take方法从缓冲区取出产品。当缓冲区满时,生产者会被阻塞等待;当缓冲区空时,消费者会被阻塞。这样可以保证生产者和消费者之间的同步,避免了资源竞争。
运行此代码,你会看到生产者和消费者的状态以及他们生产的/消耗的产品名称。注意,由于并发环境,实际输出可能会有所不同,因为多个线程同时操作。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)