零EVA

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

生产者消费者

 1 class Resource
 2 {
 3     private String name;
 4     private int count = 1;
 5     private boolean flag = false;
 6     
 7     public synchronized void set(String name)
 8     {
 9         while(flag)
10             try{this.wait();}catch(Exception e){}
11         this.name = name+"--"+count++;
12         System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);
13         flag = true;
14         this.notifyAll();
15     }
16     
17     public synchronized void out()
18     {
19         while(!flag)
20             try{this.wait();}catch(Exception e){}
21         System.out.println(Thread.currentThread().getName()+"...消费者..."+this.name);
22         flag = false;
23         this.notifyAll();
24     }
25 }
26 
27 class Producer implements Runnable
28 {
29     private Resource res;
30     
31     Producer(Resource res)
32     {
33         this.res = res;
34     }
35     
36     public void run()
37     {
38         while(true)
39         {
40             res.set("+TT+");
41         }
42     }
43 }
44 
45 class Customer implements Runnable
46 {
47     private Resource res;
48     
49     Customer(Resource res)
50     {
51         this.res = res;
52     }
53     
54     public void run()
55     {
56         while(true)
57         {
58             res.out();
59         }
60     }
61 }
62 class ProducerCustomerDemo
63 {
64     public static void main(String[] args)
65     {
66         Resource r = new Resource();
67         
68         Producer pro = new Producer(r);
69         Customer con = new Customer(r);
70         
71         Thread t1 = new Thread(pro);
72         Thread t2 = new Thread(con);
73         
74         t1.start();
75         t2.start();
76     }
77 }
View Code

 JDK 1.5及以上版本提供了多线程升级解决方法

将同步synchronized替换成显示的Lock操作

将Object中的wait,notify,notifyAll替换成了Condition对象

该对象可以对Lock锁进行获取

  1 import java.util.concurrent.locks.*;
  2 class Resource
  3 {
  4     private String name;
  5     private int count = 1;
  6     private boolean flag = false;
  7     
  8     private Lock lock = new ReentrantLock();
  9         
 10     private Condition condition_pro = lock.newCondition();
 11     private Condition condition_con = lock.newCondition();
 12     
 13     public void set(String name)throws InterruptedException
 14     {
 15         lock.lock();
 16         try
 17         {
 18             while(flag)
 19                 condition_pro.await();
 20             this.name = name+"--"+count++;
 21             System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);
 22             flag = true;
 23             condition_con.signal();
 24         }
 25         finally
 26         {
 27             lock.unlock();
 28         }
 29     }
 30     
 31     public void out() throws InterruptedException
 32     {
 33         lock.lock();
 34         try
 35         {
 36             while(!flag)
 37                 condition_con.await();
 38             System.out.println(Thread.currentThread().getName()+"...消费者..."+this.name);
 39             flag = false;
 40             condition_pro.signal();
 41         }
 42         finally
 43         {
 44             lock.unlock();
 45         }
 46     }
 47 }
 48 
 49 class Producer implements Runnable
 50 {
 51     private Resource res;
 52     
 53     Producer(Resource res)
 54     {
 55         this.res = res;
 56     }
 57     
 58     public void run()
 59     {
 60         while(true)
 61         {
 62             try
 63             {
 64                 res.set("+TT+");
 65             }
 66             catch(InterruptedException e)
 67             {
 68                 
 69             }
 70         }
 71     }
 72 }
 73 
 74 class Customer implements Runnable
 75 {
 76     private Resource res;
 77     
 78     Customer(Resource res)
 79     {
 80         this.res = res;
 81     }
 82     
 83     public void run()
 84     {
 85         while(true)
 86         {
 87             try
 88             {
 89                 res.out();
 90             }
 91             catch(InterruptedException e)
 92             {
 93                 
 94             }
 95         }
 96     }
 97 }
 98 class ProducerCustomerDemo
 99 {
100     public static void main(String[] args)
101     {
102         Resource r = new Resource();
103         
104         Producer pro = new Producer(r);
105         Customer con = new Customer(r);
106         
107         Thread t1 = new Thread(pro);
108         Thread t2 = new Thread(con);
109         Thread t3 = new Thread(pro);
110         Thread t4 = new Thread(con);
111         
112         t1.start();
113         t2.start();
114         t3.start();
115         t4.start();
116     }
117 }
View Code

 

posted on 2017-06-23 18:36  零EVA  阅读(141)  评论(0编辑  收藏  举报