生產者消費者

生產者
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();//通知消費者取出產品

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章