4、碗类SyncStack.java
package com.gaowei.PV;
public class SyncStack {
private int index=0;
ManTou[] arrMT=new ManTou[6];
//添加馒头
public synchronized void push(ManTou mt){
while (index==arrMT.length) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notifyAll();
arrMT[index]=mt;
index++;
}
//吃馒头
public synchronized ManTou pop(){
while(index==0){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notifyAll();
index--;
return arrMT[index];
}
}