手记

生产者消费者

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

}

0人推荐
随时随地看视频
慕课网APP