JDK 5.0 특성-스 레 드 조건

21971 단어 Condition
출처:http://www.cnblogs.com/taven/archive/2011/12/17/2291471.html
  1 import java.util.concurrent.ExecutorService;
  2 
  3 import java.util.concurrent.Executors;
  4 
  5 import java.util.concurrent.locks.Condition;
  6 
  7 import java.util.concurrent.locks.Lock;
  8 
  9 import java.util.concurrent.locks.ReentrantLock;
 10 
 11  
 12 
 13 /**
 14 
 15  *       lock                ,     Producer Consumer  
 16 
 17  * Java 5.0  ,      Object  wait(),notify() notifyAll()      
 18 
 19  * 5.0  ,        Condition      
 20 
 21  */
 22 
 23 /**
 24 
 25  *   Condition        :
 26 
 27  * 1.  Lock newCondition    Condition   
 28 
 29  * 2.Condition await             ,        Lock,               ,    Lock.
 30 
 31  * 3.Condition signal           Condition   .
 32 
 33  */
 34 
 35 public class ConditionTest {
 36 
 37     /**
 38 
 39      *     ,        ,            
 40 
 41      * Consumer                 ,               
 42 
 43      *   Producer              。
 44 
 45      *            Comsumer              。
 46 
 47      */
 48 
 49     public static class Basket{
 50 
 51         // 
 52 
 53         Lock lock = new ReentrantLock();
 54 
 55         //     Condition  
 56 
 57         Condition produced = lock.newCondition();
 58 
 59         Condition consumed  = lock.newCondition();
 60 
 61         //       ,   1
 62 
 63         int num = 0;
 64 
 65         //
 66 
 67         public void produce() throws InterruptedException{
 68 
 69             //   
 70 
 71             lock.lock();
 72 
 73             System.out.println("Producer get a lock...");
 74 
 75  
 76 
 77                      try{
 78 
 79                 //          
 80 
 81                 while(num == 1){
 82 
 83                     //     ,    ,   ,    
 84 
 85                     //       
 86 
 87                     System.out.println("Producer sleep...");
 88 
 89                     consumed.await();
 90 
 91                     System.out.println("Producer awaked...");
 92 
 93                 }
 94 
 95                 //    
 96 
 97                 Thread.sleep(500);
 98 
 99                 System.out.println("Producer produced an Apple.");
100 
101                 num = 1;
102 
103                 //    produced Condition   
104 
105                 produced.signal();
106 
107             }finally{
108 
109                 lock.unlock();
110 
111             }
112 
113         }
114 
115         //
116 
117         public void consume() throws InterruptedException{
118 
119             //   
120 
121             lock.lock();
122 
123             System.out.println("Consumer get a lock...");
124 
125             try{
126 
127                 //          
128 
129                 while(num == 0){
130 
131                     //      ,    ,    ,    
132 
133                     //         
134 
135                     System.out.println("Consumer sleep...");
136 
137                     produced.await();
138 
139                     System.out.println("Consumer awaked...");
140 
141                 }
142 
143                 //   
144 
145                 Thread.sleep(500);
146 
147                 System.out.println("Consumer consumed an Apple.");
148 
149                 num = 0;
150 
151                 //         consumed Condition   
152 
153                             consumed.signal();
154 
155             } finally {
156 
157                 lock.unlock();
158 
159             }
160 
161         }
162 
163     }
164 
165     //  Basket  
166 
167     public static void testBasket() throws Exception{
168 
169         final Basket basket = new Basket();
170 
171         //    producer
172 
173         Runnable producer  = new Runnable(){
174 
175             public void run() {
176 
177                 try{
178 
179                     basket.produce();
180 
181                 }catch(InterruptedException ex){
182 
183                     ex.printStackTrace();
184 
185                 }
186 
187             }           
188 
189         };
190 
191         //    consumer
192 
193         Runnable consumer = new Runnable(){
194 
195             public void run(){
196 
197                 try{
198 
199                     basket.consume();
200 
201                 }catch(InterruptedException ex){
202 
203                     ex.printStackTrace();
204 
205                 }
206 
207             }
208 
209         };
210 
211         //   3 consumer producer
212 
213         ExecutorService service = Executors.newCachedThreadPool();
214 
215         for(int i = 0; i <3; i++){
216 
217             service.submit(producer);
218 
219         }
220 
221         for(int i = 0;i<3;i++){
222 
223             service.submit(consumer);
224 
225         }
226 
227         service.shutdown();
228 
229     }
230 
231     public static void main(String... args)throws Exception{
232 
233         ConditionTest.testBasket();
234 
235     }
236 
237 }
238 
239  

좋은 웹페이지 즐겨찾기