多线程 多生产者 多消费者 交替生成不同食物 消费者交替消费不同食物

  1 import java.util.concurrent.locks.Condition;
  2 import java.util.concurrent.locks.Lock;
  3 import java.util.concurrent.locks.ReentrantLock;
  4 /*
  5  * 多生产者     多消费者
  6  */
  7 public class producer_consumer {
  8     public static void main(String[] args) {
  9         Product p = new Product();
 10         Thread t0 = new Thread(new Producer(p));
 11         Thread t1 = new Thread(new Producer(p));
 12         Thread t2 = new Thread(new Consumer(p));
 13         Thread t3 = new Thread(new Consumer(p));
 14         t0.start();
 15         t1.start();
 16         t2.start();
 17         t3.start();
 18     }
 19 }
 20 
 21 class Consumer implements Runnable {
 22     private Product p;
 23 
 24     public Consumer(Product p) {
 25         super();
 26         this.p = p;
 27     }
 28 
 29     @Override
 30     public void run() {
 31             p.get();
 32     }
 33 }
 34 
 35 class Producer implements Runnable {
 36     private Product p;
 37 
 38     public Producer(Product p) {
 39         super();
 40         this.p = p;
 41     }
 42 
 43     @Override
 44     public void run() {        
 45             p.set();
 46     }
 47 
 48 }
 49 
 50 class Product {
 51     private String name;
 52     private String color;
 53     boolean flag = false;
 54 
 55     public Product() {
 56         super();
 57     }
 58 
 59     private int pos;
 60     Lock l = new ReentrantLock();//创建一个锁对象
 61     Condition producer_c = l.newCondition();//创建一个生产者监视器
 62     Condition consumer_c = l.newCondition();//消费者监视器
 63 
 64     public void set() {
 65 
 66         while (true) {
 67             l.lock();//加上锁
 68             while (flag) {
 69                 try {
 70                     producer_c.await();
 71                 } catch (InterruptedException e) {
 72                     e.printStackTrace();
 73                 }
 74             }
 75             if (pos % 2 == 0) {
 76                 this.setName("馒头");
 77                 this.setColor("白色");
 78             } else {
 79                 this.setName("玉米");
 80                 this.setColor("黄色");
 81             }
 82             System.out.println(Thread.currentThread().getName()+"生产者生产============"+this.getName()+" *** "+this.getColor());
 83             pos++;
 84             flag = true;
 85             consumer_c.signal();//唤醒消费者
 86             l.unlock();//释放锁
 87         }
 88     }
 89 
 90     public void get() {
 91         while (true) {
 92             l.lock();
 93             while (!flag) {
 94                 try {
 95                     consumer_c.await();
 96                 } catch (InterruptedException e) {
 97                     e.printStackTrace();
 98                 }
 99             }
100             System.out.println(Thread.currentThread().getName() + "..消费者消费..."
101                     + this.getName() + "===" + this.getColor());
102             flag=false;
103             producer_c.signal();//唤醒生产者
104             l.unlock();
105         }
106     }
107 
108     public Product(String name, String color) {
109         super();
110         this.name = name;
111         this.color = color;
112     }
113 
114     public String getName() {
115         return name;
116     }
117 
118     public void setName(String name) {
119         this.name = name;
120     }
121 
122     public String getColor() {
123         return color;
124     }
125 
126     public void setColor(String color) {
127         this.color = color;
128     }
129 
130 }

 

posted @ 2015-07-15 22:56  凤栖梧桐镇  阅读(203)  评论(0)    收藏  举报