V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
overthemoon
V2EX  ›  Java

问一个 Java 的 Condition 问题

  •  
  •   overthemoon · 2020-12-03 10:45:43 +08:00 · 1810 次点击
    这是一个创建于 1451 天前的主题,其中的信息可能已经有所发展或是发生改变。

    有两个线程,一个生产者,一个消费者,不断的在队列里面插数据和取数据。 有个问题 notFull.signalAll()这句代码一个放在 try 里面,一个放在 finally 里面控制台输出结果为什么不一样?

    
    public class ConditionDemo2 {
    
        private int queueSize = 10;
        private PriorityQueue<Integer> queue = new PriorityQueue<Integer>(queueSize);
        private Lock lock = new ReentrantLock();
        private Condition notFull = lock.newCondition();
        private Condition notEmpty = lock.newCondition();
    
        public static void main(String[] args) {
            ConditionDemo2 conditionDemo2 = new ConditionDemo2();
            Producer producer = conditionDemo2.new Producer();
            Consumer consumer = conditionDemo2.new Consumer();
            producer.start();
            consumer.start();
        }
    
        class Consumer extends Thread {
    
            @Override
            public void run() {
                consume();
            }
    
            private void consume() {
                while (true) {
                    lock.lock();
                    try {
                        while (queue.size() == 0) {
                            System.out.println("队列空,等待数据");
                            try {
                                notEmpty.await();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        queue.poll();
                        notFull.signalAll();//看这里----------------------
                        System.out.println("从队列里取走了一个数据,队列剩余" + queue.size() + "个元素");
                    } finally {
                        lock.unlock();
                    }
                }
            }
        }
    
        class Producer extends Thread {
    
            @Override
            public void run() {
                produce();
            }
    
            private void produce() {
                while (true) {
                    lock.lock();
                    try {
                        while (queue.size() == queueSize) {
                            System.out.println("队列满,等待有空余");
                            try {
                                notFull.await();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        queue.offer(1);
                        notEmpty.signalAll();//看这里----------------------
                        System.out.println("向队列插入了一个元素,队列剩余空间" + (queueSize - queue.size()));
                    } finally {
                        lock.unlock();
                    }
                }
            }
        }
    
    }
    
    
    
    
    public class ConditionDemo3 {
    
        private int queueSize = 10;
        private PriorityQueue<Integer> queue = new PriorityQueue<Integer>(queueSize);
        private Lock lock = new ReentrantLock();
        private Condition notFull = lock.newCondition();
        private Condition notEmpty = lock.newCondition();
    
        public static void main(String[] args) {
            ConditionDemo3 conditionDemo2 = new ConditionDemo3();
            Producer producer = conditionDemo2.new Producer();
            Consumer consumer = conditionDemo2.new Consumer();
            producer.start();
            consumer.start();
        }
    
        class Consumer extends Thread {
    
            @Override
            public void run() {
                consume();
            }
    
            private void consume() {
                while (true) {
                    lock.lock();
                    try {
                        while (queue.size() == 0) {
                            System.out.println("队列空,等待数据");
                            try {
                                notEmpty.await();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        queue.poll();
                        System.out.println("从队列里取走了一个数据,队列剩余" + queue.size() + "个元素");
                    } finally {
                        notFull.signalAll();//看这里----------------------
                        lock.unlock();
                    }
                }
            }
        }
    
        class Producer extends Thread {
    
            @Override
            public void run() {
                produce();
            }
    
            private void produce() {
                while (true) {
                    lock.lock();
                    try {
                        while (queue.size() == queueSize) {
                            System.out.println("队列满,等待有空余");
                            try {
                                notFull.await();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        queue.offer(1);
                        System.out.println("向队列插入了一个元素,队列剩余空间" + (queueSize - queue.size()));
                    } finally {
                        System.out.println("final");
                        notFull.signalAll();//看这里----------------------
                        lock.unlock();
                    }
                }
            }
        }
    
    }
    
    
    
    3 条回复    2020-12-03 16:12:48 +08:00
    overthemoon
        1
    overthemoon  
    OP
       2020-12-03 10:54:17 +08:00
    此贴终结,我代码看错了==~~~~~~~~
    bxb100
        2
    bxb100  
       2020-12-03 13:12:43 +08:00
    @overthemoon #1 分享一下
    zcsz
        3
    zcsz  
       2020-12-03 16:12:48 +08:00
    @overthemoon 亏我点进来从上到下仔仔细细看了一遍,拉到 1 楼..............
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1045 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 23:20 · PVG 07:20 · LAX 15:20 · JFK 18:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.