转:http://blog.csdn.net/a352193394/article/details/39454157
我们在多线程开发中,可能会出现这种情况。就是一个线程需要另外一个线程满足某某条件才能继续运行,或者需
要其他线程满足好几个条件才能运行,对于这样的多条件的多线程并发,我们如何控制好各个线程之间的关系,使他们
能很好的处理冲突不至于相互出现问题呢,下面我们来介绍一下Java提供的Condition这个接口,这个接口很好的实现了
这种需求。
对于这个问题最经典的例子就是生产者消费者模型,生产者当缓冲区满的时候不生产商品知道缓冲区有空余,消费
者当缓冲区为0 的时候不拿商品,直到生产者向缓冲区放入商品,下面我们使用Conditon这个接口来实现这样的需求。
 
- import java.util.concurrent.ExecutorService;  
- import java.util.concurrent.Executors;  
- import java.util.concurrent.locks.Condition;  
- import java.util.concurrent.locks.Lock;  
- import java.util.concurrent.locks.ReentrantLock;  
-   
- public class ConditionTest {  
-   
-     
-     public static class Basket {  
-         
-         Lock lock = new ReentrantLock();  
-         
-         Condition produced = lock.newCondition();  
-         Condition consumed = lock.newCondition();  
-         
-         int num = 0;  
-   
-         
-         public void produce() throws InterruptedException {  
-             
-             lock.lock();  
-             System.out.println("Producer get a lock...");  
-             try {  
-                 
-                 while (num == 1) {  
-                     
-                     
-                     System.out.println("Producer sleep...");  
-                     consumed.await();   
-                     System.out.println("Producer awaked...");  
-                 }  
-                 
-                 Thread.sleep(500);  
-                 System.out.println("Producer produced an Apple.");  
-                 num = 1;  
-                 
-                 produced.signal();  
-             } finally {  
-                 lock.unlock();  
-             }  
-         }  
-         
-         public void consume() throws InterruptedException {  
-             
-             lock.lock();  
-             System.out.println("Consumer get a lock...");  
-             try {  
-                 
-                 while (num == 0) {  
-                     
-                     
-                     System.out.println("Consumer sleep...");  
-                     produced.await();    
-                     System.out.println("Consumer awaked...");  
-                 }  
-                 
-                 Thread.sleep(500);  
-                 System.out.println("Consumer consumed an Apple.");  
-                 num = 0;  
-                 
-                 consumed.signal();  
-             } finally {  
-                 lock.unlock();  
-             }  
-         }  
-     }  
-     
-     public static void testBasket() throws Exception {  
-         final Basket basket = new Basket();  
-         
-         Runnable producer = new Runnable() {  
-             public void run() {  
-                 try {  
-                     basket.produce();  
-                 } catch (InterruptedException ex) {  
-                     ex.printStackTrace();  
-                 }  
-             }  
-         };  
-   
-         
-         Runnable consumer = new Runnable() {  
-             public void run() {  
-                 try {  
-                     basket.consume();  
-                 } catch (InterruptedException ex) {  
-                     ex.printStackTrace();  
-                 }  
-             }  
-         };  
-   
-         
-         ExecutorService service = Executors.newCachedThreadPool();  
-         for (int i = 0; i < 3; i++){  
-             service.submit(producer);  
-         }  
-         for (int i = 0; i < 3; i++){  
-             service.submit(consumer);  
-         }  
-         service.shutdown();  
-     }        
-   
-     public static void main(String[] args) throws Exception {  
-         ConditionTest.testBasket();  
-     }  
- }  
 
 
Java之多线程开发时多条件Condition接口的使用
原文:http://www.cnblogs.com/evertriely/p/4912573.html