public class ProduceConsume {
public static void main(String[] args) {
SyncStack ss = new SyncStack();
Producer p = new Producer(ss);
Consume c = new Consume(ss);
Thread tp = new Thread(p);
Thread tc = new Thread(c);
tp.start();
tc.start();
}
}
class SteamBread{
int id;
SteamBread(int id){
this.id = id;
}
public String toString(){
return "steamBread:"+id;
}
}
class SyncStack{
int index = 0;
SteamBread[] stb = new SteamBread[6];
public synchronized void push(SteamBread sb){
while(index==stb.length){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notify();
stb[index] = sb;
this.index++;
}
public synchronized SteamBread pop(){
while(index==0){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notify();
this.index--;
return stb[index];
}
}
class Producer implements Runnable{
SyncStack ss = null;
Producer(SyncStack ss){
this.ss = ss;
}
@Override
public void run() {
for(int i=0;i<20;i++){
SteamBread stb = new SteamBread(i);
ss.push(stb);
System.out.println("生产了"+stb);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Consume implements Runnable{
SyncStack ss = null;
public Consume(SyncStack ss) {
super();
this.ss = ss;
}
@Override
public void run() {
for(int i=0;i<20;i++){
SteamBread stb = ss.pop();
System.out.println("消费了"+stb);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}