V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
overthemoon
V2EX  ›  Java

问一个 Java 的 Condition 问题

  •  
  •   overthemoon · Dec 3, 2020 · 2398 views
    This topic created in 1977 days ago, the information mentioned may be changed or developed.

    有两个线程,一个生产者,一个消费者,不断的在队列里面插数据和取数据。 有个问题 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 replies    2020-12-03 16:12:48 +08:00
    overthemoon
        1
    overthemoon  
    OP
       Dec 3, 2020
    此贴终结,我代码看错了==~~~~~~~~
    bxb100
        2
    bxb100  
       Dec 3, 2020
    @overthemoon #1 分享一下
    zcsz
        3
    zcsz  
       Dec 3, 2020
    @overthemoon 亏我点进来从上到下仔仔细细看了一遍,拉到 1 楼..............
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2363 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 52ms · UTC 10:32 · PVG 18:32 · LAX 03:32 · JFK 06:32
    ♥ Do have faith in what you're doing.