java模拟生产者消费者

  1 public class Main{
  2 
  3     public static Num n = new Num();
  4     
  5     public static void main(String[] args) {
  6         Produce p1 = new Produce(n);
  7         Produce p2 = new Produce(n);
  8         Consume c1 = new Consume(n);
  9         Consume c2 = new Consume(n);
 10         
 11         //下面是两个生产者,两个消费者一起生产和消费
 12         new Thread(p1).start();
 13         new Thread(p2).start();
 14         new Thread(c1).start();
 15         new Thread(c2).start();
 16         
 17         //运行100ms后停止所有线程
 18         try {
 19             Thread.sleep(100);
 20         } catch (InterruptedException e) {
 21             e.printStackTrace();
 22         }
 23         p1.stop();
 24         p2.stop();
 25         c1.stop();
 26         c2.stop();
 27     }
 28 
 29 }
 30 
 31 /**
 32  * 共享资源类l
 33  */
 34 class Num{
 35     int index=0;
 36     
 37     /**
 38      * 生产方法
 39      * 这里一定要加synchronized关键字,否则会报 IllegalMonitorStateException 异常,因为不加同步关键字的话,则当前线程没有线程锁,无法调用wait() 和 notifyAll()
 40      */
 41     public synchronized void produce(){
 42         //这里一定要 是while 判断,不能用if, 否则多个生产者多个消费者可能出现-1或11的情况
 43         while(index>=10){
 44             try {
 45                 this.wait();
 46             } catch (InterruptedException e) {
 47                 e.printStackTrace();
 48             }
 49         }
 50         index++;
 51         System.out.println("生产了一个,现在是"+index);
 52         notifyAll();
 53     }
 54     
 55     /**
 56      * 消费方法
 57      */
 58     public synchronized void consumer(){
 59         while(index<=0){
 60             try {
 61                 this.wait();
 62             } catch (InterruptedException e) {
 63                 e.printStackTrace();
 64             }
 65         }
 66         index--;
 67         System.out.println("消费了一个,现在是"+index);
 68         notifyAll();
 69     }
 70 }
 71 
 72 /**
 73  *
 74  *    生产者线程
 75  */
 76 class Produce implements Runnable{
 77     
 78     Num n;
 79     boolean flag = true;
 80     
 81     public Produce(Num n){
 82         this.n = n;
 83     }
 84     
 85     public void run(){
 86         while(flag){
 87             n.produce();
 88         }
 89     }
 90     
 91     public void stop(){
 92         flag = false;
 93     }
 94 }
 95 
 96 /**
 97  * 消费者线程
 98  */
 99 class Consume implements Runnable{
100     
101     private Num n;
102     private boolean flag = true;
103     
104     public Consume(Num n){
105         this.n = n;
106     }
107     
108     public void run(){
109         
110         while(flag){
111             n.consumer();
112         }
113     }
114     
115     /**
116      * 停止方法,改变run() 方法中标志位达到停止线程的效果
117      */
118     public void stop(){
119         flag = false;
120     }
121 }

 

转载于:https://www.cnblogs.com/zlzlzl/p/4853622.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值