码迷,mamicode.com
首页 > 其他好文 > 详细

生产者消费者

时间:2018-08-17 10:15:42      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:tst   notify   produce   nts   void   product   not   rod   sync   

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

}

生产者消费者

标签:tst   notify   produce   nts   void   product   not   rod   sync   

原文地址:http://blog.51cto.com/13919712/2160905

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!