java线程锁的condtion学习笔记

一。java

    1.有时线程取得锁时须要在必定条件下才能作某些工做,好比经典的Producer、Consumer问题。app

    2.在jdk 1.5千,这种功能是经过Object类的wait notify和notifyall实现的,以后能够用condition实现。ide

二。测试

    测试代码以下,注意注释部分代码。或者直接参考arrayblickingqueue的源码put和take方法线程

public class ConditonTest {
    public static class Basket {
        Lock lock = new ReentrantLock();
        Condition produceCondition = lock.newCondition();
        Condition consumeCondition = lock.newCondition();
        int num = 0;

        public void consume() throws InterruptedException {
            lock.lock();
            System.out.println("consumer get a lock");
            try {
                while (num == 0) {
                    // 等于0的话不消费苹果
                    System.out.println("Consumer sleep");
                    try {
                        consumeCondition.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("consumer awaked");
                }
                Thread.sleep(500);
                System.out.println("consumer consumed an apple");
                num = 0;
                produceCondition.signalAll();

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                System.out.println("lock unlock");
                lock.unlock();
            }

        }

        public void produce() throws InterruptedException {
            lock.lock();
            System.out.println("producer get a lock");
            try {
                while (num == 1) {
                    // 等于1的话不生产苹果
                    System.out.println("producer sleep");
                    try {
                        produceCondition.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("producer awaked");
                }
                Thread.sleep(500);
                System.out.println("producer produced an apple");
                num = 1;
                consumeCondition.signalAll();

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                System.out.println("lock unlock");
                lock.unlock();
            }
        }
    }
    public static void testBasket(){
        final Basket bask=new Basket();
        Runnable producer=new Runnable() {
            
            @Override
            public void run() {
                try {
                    bask.produce();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        Runnable consumer=new Runnable() {
            
            @Override
            public void run() {
                try {
                    bask.consume();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
        /*ExecutorService executorService=Executors.newCachedThreadPool();
        for(int i=0;i<3;i++){
            executorService.submit(consumer);
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for(int i=0;i<3;i++){
            executorService.submit(producer);
        }*/
        new Thread(consumer).start();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        new Thread(producer).start();
    }
    public static void main(String[] args) {
        testBasket();
    }
}
相关文章
相关标签/搜索