Condition

Condition进一步封装了Object的wait  notify等方法

public class Bussiness {
    
    private boolean bool = true;
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();
    
    public void main(int loop) throws InterruptedException {
        lock.lock();
        try{
            while(bool){
                //wait()方法
                condition.await();
            }
            for(int i = 0;i < 100;i++){
                System.out.println("main thread seq of "+i+", loop of "+loop);
            }
            bool = true;
            //notify()方法
            condition.signal();
        }finally{
            lock.unlock();
        }
    }

    public void sub(int loop) throws InterruptedException{
        lock.lock();
        try{
            while(!bool){
                condition.await();
            }
            for(int i  =0;i<10;i++){
                System.out.println("sub thread seq of "+i+", loop of "+ loop);
            }
            bool = false;
            condition.signal();
        }finally{
            lock.unlock();
        }     
    }
}
public class ThreadTest {
    public static void main(String[] args) {
        final Bussiness bussiness = new Bussiness();
        new Thread(new Runnable(){
            public void run() {
                threadExecute(bussiness, "sub");
            }
        }).start();
        threadExecute(bussiness, "main");
    }

    public static void threadExecute(Bussiness bussiness,String threadName){
        for(int i = 0; i < 100;i++){
            try{
                if("main".equals(threadName)){
                    bussiness.main(i);
                }else{
                    bussiness.sub(i);
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}

Condition可以为多个线程建立不同的condition

/**
 * 多个condition的好处:
 * @author hp
 *
 */
public class BoundedBuffer {
    
    final Lock lock = new ReentrantLock();    //锁对象
    final Condition notFull = lock.newCondition();//写线程条件
    final Condition notEmpty = lock.newCondition();//读线程条件
    
    //缓存队列
    final Object[] items = new Object[100];
    int putptr/*写索引*/,takeptr/*读索引*/,count/*队列中存在的数据个数*/;
    
    public void put(Object x) throws InterruptedException{
        lock.lock();
        try{
            while(count == items.length){
                notFull.wait();
            }
            items[putptr] = x;
            if(++putptr == items.length)    //如果写索引到了队列的最后一个位置,那么就置为0
                putptr=0;
            ++count;
            notEmpty.signal();
        }finally{
            lock.unlock();
        }
    }
    
    
    public Object take() throws InterruptedException{
        lock.lock();
        try{
            //如果队列中为空,则唤醒写线程
            while(count == 0){
                notEmpty.await();
            }
            Object x = items[takeptr];
            if(++takeptr == items.length)
                takeptr = 0;
            --count;
            notFull.signal();
            return x;
        }finally{
            lock.unlock();
        }
        
    }
    
}
原文地址:https://www.cnblogs.com/lfdingye/p/7445247.html