Thread--生产者消费者

2个生产者,2个消费者,库存容量2

 1 package p_c_allWait.copy;
 2 
 3 import java.util.LinkedList;
 4 import java.util.List;
 5 
 6 public class ValueObject {
 7     
 8     public static final int MAX = 2;
 9     
10 //    public static String value = "";
11     
12     public static List<String> list = new LinkedList<String>();
13 
14 }
 1 package p_c_allWait.copy;
 2 
 3 public class P {
 4     
 5     private String lock;
 6 
 7     public P(String lock) {
 8         super();
 9         this.lock = lock;
10     }
11     
12     public void setValue() {
13         try {
14             synchronized (lock) {
15                 while(ValueObject.list.size() >= ValueObject.MAX) {
16                     System.out.println("生产者 " + Thread.currentThread().getName() + " WAITING 了 库存 : " + ValueObject.list.size());
17                     lock.wait();
18                 }
19                 String value = System.currentTimeMillis() + "_" + System.nanoTime();
20                 ValueObject.list.add(value);
21                 System.out.println("生产者 " + Thread.currentThread().getName() + " RUNNABLE 了 库存 : " + ValueObject.list.size());
22                 lock.notifyAll();
23             }
24         } catch (Exception e) {
25             // TODO: handle exception
26             e.printStackTrace();
27         }
28     }
29 
30 }
 1 package p_c_allWait.copy;
 2 
 3 public class C {
 4     
 5     private String lock;
 6 
 7     public C(String lock) {
 8         super();
 9         this.lock = lock;
10     }
11     
12     public void getValue() {
13         try {
14             synchronized (lock) {
15                 while(ValueObject.list.size()<=0) {
16                     System.out.println("消费者 " + Thread.currentThread().getName() + " wating 了 库存 : " + ValueObject.list.size());
17                     lock.wait();
18                 }
19                 ValueObject.list.remove(0);
20                 System.out.println("消费者 " + Thread.currentThread().getName() + " RUNNABLE 了 库存 : " + ValueObject.list.size());
21                 lock.notifyAll();
22             }
23         } catch (Exception e) {
24             // TODO: handle exception
25             e.printStackTrace();
26         }
27     }
28 
29 }
 1 package p_c_allWait.copy;
 2 
 3 public class ThreadP extends Thread {
 4 
 5     private P p;
 6 
 7     public ThreadP(P p) {
 8         super();
 9         this.p = p;
10     }
11 
12     @Override
13     public void run() {
14         while(true) {
15             p.setValue();
16         }
17     }
18 
19 }
 1 package p_c_allWait.copy;
 2 
 3 public class ThreadC extends Thread {
 4 
 5     private C c;
 6 
 7     public ThreadC(C c) {
 8         super();
 9         this.c = c;
10     }
11 
12     @Override
13     public void run() {
14         while(true) {
15             c.getValue();
16         }
17     }
18     
19     
20     
21 }
 1 package p_c_allWait.copy;
 2 
 3 public class Run {
 4     
 5     public static void main(String[] args) {
 6         String lock = "";
 7         P p = new P(lock);
 8         C r = new C(lock);
 9         ThreadP[] pThread = new ThreadP[2];
10         ThreadC[] cThread = new ThreadC[2];
11         for(int i=0; i<2; i++) {
12             pThread[i] = new ThreadP(p);
13             pThread[i].setName("生产者 " + (i+1));
14             cThread[i] = new ThreadC(r);
15             cThread[i].setName("消费者 " + (i+1));
16             pThread[i].start();
17             cThread[i].start();
18         }
19     }
20 
21 }

 

posted @ 2017-02-06 10:01  MicroCat  阅读(175)  评论(0编辑  收藏  举报