package com.test.Thread;
public class TestPC {
public static void main(String[] args) {
SysContainer container =new SysContainer();
new Productor(container).start();
new Consumer(container).start();
}
}
class Productor extends Thread{
SysContainer container;
public Productor(SysContainer container) {
this.container = container;
}
@Override
public void run(){
for (int i = 0; i < 100; i++) {
System.out.println("生产了"+i+"只鸡");
container.push(new Chicken(i));
}
}
}
class Consumer extends Thread{
SysContainer container;
public Consumer(SysContainer container) {
this.container = container;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("消费了-->"+container.pop().id+"只鸡");
}
}
}
class Chicken{
int id;
public Chicken(int id){
this.id=id;
}
}
class SysContainer{
Chicken[] chickens=new Chicken[9];
int count=0;
public synchronized void push(Chicken chicken){
if(count==chickens.length){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
chickens[count]=chicken;
count++;
this.notifyAll();
}
public synchronized Chicken pop(){
if(count==0){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
count--;
Chicken chicken =chickens[count];
this.notifyAll();
return chicken;
}
}