#生产者和消费者应当共用container的count,但是会偶尔出现数组下标越界和count不匹配
import java.awt.*;
public class Part6_TestProducerConsumer_1 {
public static void main(String[] args) {
//生产者和消费者必须共用一个容器
SynContainer container = new SynContainer();
Producer producer = new Producer(container);
Consumer consumer = new Consumer(container);
new Thread(producer,"生产者").start();
new Thread(consumer, "消费者").start();
}
}
class Chicken{
int id;//鸡的序列号
public Chicken(int id) {
this.id = id;
}
}
class Producer implements Runnable{
SynContainer container;
public Producer(SynContainer container) {
this.container = container;
}
@Override
public void run() {
for (int i = 1; i < 100; i++) {
try {
container.putIn(new Chicken(i));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("生产者生产了第" + i + "只鸡," + "仓库中还有" + container.count + "只鸡");
}
}
}
class Consumer implements Runnable{
SynContainer container;
public Consumer(SynContainer container) {
this.container = container;
}
@Override
public void run() {
for (int i = 1; i < 100; i++) {
try {
container.takeOut();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("消费者消费了第" + i + "只鸡," + "仓库中还有" + container.count + "只鸡");
}
}
}
class SynContainer{
Chicken[] chickens = new Chicken[10];//定义容器中可以装鸡的数量
int count = 0;//记录目前容器持有数量
public synchronized void putIn(Chicken chicken) throws InterruptedException {
//如果容器已满,停止生产,等待消费
if(count == chickens.length){
this.wait();
}
//如果容器不满
chickens[count] = chicken;
count ++;
//通知消费者消费
this.notifyAll();
}
public synchronized Chicken takeOut() throws InterruptedException {
//如果容器为空,停止消费,等待生产
if(count == 0){
this.wait();
}
//如果容器不为空
Chicken getChicken = chickens[count];
count --;
//通知生产者生产
this.notifyAll();
return getChicken;
}
}