LinkedBlockingQueue 与 ConcurrentLinkedQueue [英] LinkedBlockingQueue vs ConcurrentLinkedQueue

查看:26
本文介绍了LinkedBlockingQueue 与 ConcurrentLinkedQueue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与这个问题有关 之前问过.在我使用队列在生产者和消费者线程之间进行通信的情况下,人们通常会推荐使用 LinkedBlockingQueueConcurrentLinkedQueue?

My question relates to this question asked earlier. In situations where I am using a queue for communication between producer and consumer threads would people generally recommend using LinkedBlockingQueue or ConcurrentLinkedQueue?

使用其中一种的优点/缺点是什么?

What are the advantages / disadvantages of using one over the other?

我从 API 的角度看到的主要区别是 LinkedBlockingQueue 可以选择有界.

The main difference I can see from an API perspective is that a LinkedBlockingQueue can be optionally bounded.

推荐答案

对于生产者/消费者线程,我不确定 ConcurrentLinkedQueue 甚至是一个合理的选择 - 它没有实现 BlockingQueue,这是 IMO 生产者/消费者队列的基本接口.你必须调用 poll(),如果你没有找到任何东西,请稍等一下,然后再次轮询等等......空的(由于不必要地从睡眠中醒来).

For a producer/consumer thread, I'm not sure that ConcurrentLinkedQueue is even a reasonable option - it doesn't implement BlockingQueue, which is the fundamental interface for producer/consumer queues IMO. You'd have to call poll(), wait a bit if you hadn't found anything, and then poll again etc... leading to delays when a new item comes in, and inefficiencies when it's empty (due to waking up unnecessarily from sleeps).

来自 BlockingQueue 的文档:

From the docs for BlockingQueue:

BlockingQueue 实现主要用于生产者-消费者队列

BlockingQueue implementations are designed to be used primarily for producer-consumer queues

我知道它并没有严格地说只有阻塞队列应该用于生产者-消费者队列,但即便如此......

I know it doesn't strictly say that only blocking queues should be used for producer-consumer queues, but even so...

这篇关于LinkedBlockingQueue 与 ConcurrentLinkedQueue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆