首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LinkedBlockingQueue程序不终止

LinkedBlockingQueue程序不终止
EN

Stack Overflow用户
提问于 2014-06-26 18:33:09
回答 2查看 113关注 0票数 0

如果我运行以下程序,JVM将不会在执行后终止。但是,如果我将行(// newFixedThreadPool.execute(new Producer3());)从代码中取消注释,程序将在执行后终止。我知道,由于队列的阻塞性质,程序不会终止。在下面的代码中,代码的哪一部分阻止JVM的终止?

代码语言:javascript
复制
public class LinkedBlockingQueueExample {

    public static void main(String[] args) {

      final BlockingQueue<String> blockingQueue = new LinkedBlockingQueue<String>(5);

      final class Producer implements Runnable {

        @Override
        public void run() {

            try {
                blockingQueue.put("Joshua");
                blockingQueue.put("Bloch");
                System.out.println("Put Joshua in the queue");
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

      }

      final class Producer1 implements Runnable {

        @Override
        public void run() {

            try {
                blockingQueue.put("Martin");
                blockingQueue.put("Fowler");
                System.out.println("Put Mr Fowler in the Queue");
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

      }

      final class Producer3 implements Runnable {

        @Override
        public void run() {

            try {
                blockingQueue.put("Malcom");
                blockingQueue.put("Gladwell");
                System.out.println("Put an Outlier in the Queue");
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }

    final class Consumer implements Runnable {

        @Override
        public void run() {
            try {
                System.out.println(getClass() + " " + blockingQueue.take());
                System.out.println(getClass() + " " + blockingQueue.take());
                System.out.println(getClass() + " " + blockingQueue.take());
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    final class Consumer1 implements Runnable {

        @Override
        public void run() {
            try {
                System.out.println(getClass() + " " + blockingQueue.take());
                System.out.println(getClass() + " " + blockingQueue.take());
                System.out.println(getClass() + " " + blockingQueue.take());
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(5);

    newFixedThreadPool.execute(new Producer());
    newFixedThreadPool.execute(new Producer1());
    // newFixedThreadPool.execute(new Producer3());
    newFixedThreadPool.execute(new Consumer());
    newFixedThreadPool.execute(new Consumer1());

    newFixedThreadPool.shutdown();

  }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-06-26 18:36:38

额外的“索取”阻碍了终止。它阻塞了额外的“拿走”

票数 0
EN

Stack Overflow用户

发布于 2014-07-01 11:22:37

Take()调用一直处于阻塞状态,直到元素可用。如果您不想阻止,那么用户poll()

代码语言:javascript
复制
    poll()
Retrieves and removes the head of this queue, or returns null if this queue is empty.

参考资料:http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingQueue.html#poll()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24437736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档