生產者
public void synchronized produce(){
if(this.product>MAX_PRODUCT){
try{
wait();
}catch(InterruptedException e){
e.printStackTrace();
}
return;
}
this.product++;//如果沒有大於生產最大上限,則進行生產
notifyAll();//通知消費者取出產品
}
消費者
public void synchronized resume(){
if(this.product<MIN_PRODUCT){//小於最小產品量
try{
wait();
}catch(InterruptedException e){
e.printStackTrace();
}
return;
}
this.product--;//如果沒有大於生產最大上限,則進行生產
notifyAll();//通知消費者取出產品
}