130

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?

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

6 Answers 6

122

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).

From the docs for 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...

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Jon - I hadn't noticed that. So where / why would you use ConcurrentLinkedQueue?
When you need to access the queue from a lot of threads, but you don't need to "wait" on it.
A ConcurrentLinkedQueue is also useful if your thread is checking multiple queues. For instance, in a multi-tenant server. Assuming for isolation reasons you don't use a single blocking queue and a tenant discriminator instead.
your case only hold true if we use bounded queue, in unbounded queue take() and put() merely consumes extra resource( interms of synchronization ) than ConcurrentLinkedQueue . although it is the case to use bounded queues for Producer-consumer scenarios
@Adamski IMO, ConcurrentLinkedQueue is just a linkedlist to be used in a multi threaded environment. The best analogy for this would be ConcurrentHashMap and HashMap.
80

This question deserves a better answer.

Java's ConcurrentLinkedQueue is based on the famous algorithm by Maged M. Michael and Michael L. Scott for non-blocking lock-free queues.

"Non-blocking" as a term here for a contended resource (our queue) means that regardless of what the platform's scheduler does, like interrupting a thread, or if the thread in question is simply too slow, other threads contending for the same resource will still be able to progress. If a lock is involved for example, the thread holding the lock could be interrupted and all threads waiting for that lock would be blocked. Intrinsic locks (the synchronized keyword) in Java can also come with a severe penalty for performance - like when biased locking is involved and you do have contention, or after the VM decides to "inflate" the lock after a spin grace period and block contending threads ... which is why in many contexts (scenarios of low/medium contention), doing compare-and-sets on atomic references can be much more efficient and this is exactly what many non-blocking data-structures are doing.

Java's ConcurrentLinkedQueue is not only non-blocking, but it has the awesome property that the producer does not contend with the consumer. In a single producer / single consumer scenario (SPSC), this really means that there will be no contention to speak of. In a multiple producer / single consumer scenario, the consumer will not contend with the producers. This queue does have contention when multiple producers try to offer(), but that's concurrency by definition. It's basically a general purpose and efficient non-blocking queue.

As for it not being a BlockingQueue, well, blocking a thread to wait on a queue is a freakishly terrible way of designing concurrent systems. Don't. If you can't figure out how to use a ConcurrentLinkedQueue in a consumer/producer scenario, then just switch to higher-level abstractions, like a good actor framework.

9 Comments

Per your last paragraph, why do you say that waiting on a queue is a terrible way of designing concurrent systems? If we have a threadgroup with 10 threads eating tasks from a taskqueue, what's wrong with blocking when the taskqueue has less than 10 tasks?
@AlexandruNedelcu You can't make a sweeping statement like "freakishly terrible" where very often the very actor frameworks you say to use use threadpools which themselves you BlockingQueue's. If you need a highly reactive system and you know how to deal with backpressure (something that blocking queues mitigate) than nonblocking is clearly superior. But.. often times blocking IO and blocking queues can out perform nonblocking particularly if you have long running tasks that are IO bound and cannot be divide n' conquered.
@AdamGent - The actor frameworks do have implementation of mailboxes based on blocking queues, but that's a bug in my opinion, because blocking doesn't work over asynchronous boundaries and thus only works in demos. For me this has been a source of frustration, as for example Akka's notion of dealing with overflow is to block, instead of to drop messages, until version 2.4 that is, which isn't out yet. That said I do not believe that there are use-cases for which blocking queues can be superior. You're also conflating two things that shouldn't be conflated. I haven't spoken about blocking I/O.
@AlexandruNedelcu while I generally agree with you on backpressure I have yet to see a "lock free" system from top to bottom. Somewhere in a technology stack whether its Node.js, Erlang, Golang, its using a some sort of wait strategy whether that is a blockingqueue (locks) or CAS spinning its blocking and some cases a traditional lock strategy is faster. Its very hard to not have locks because of consistency and this is especially important with blocking io and schedulers which are ~ Producer/Consumer. ForkJoinPool works with short running tasks and it still has CAS spinning locks.
@AlexandruNedelcu I guess if you can show me how you can use a ConcurrentLinkedQueue (which is not bounded btw hence my weak backpressure argument) for Producer/Consumer pattern which is a pattern needed for schedulers and threadpooling I think I will give in and admit that BlockingQueue's should never be used (and you can't cheat and delegate to something else doing the scheduling ie akka since that in turn will do the blocking/waiting as it is a producer/consumer).
|
40

LinkedBlockingQueue blocks the consumer or the producer when the queue is empty or full and the respective consumer/producer thread is put to sleep. But this blocking feature comes with a cost: every put or take operation is lock contended between the producers or consumers (if many), so in scenarios with many producers/consumers the operation might be slower.

ConcurrentLinkedQueue is not using locks, but CAS, on its add/poll operations potentially reducing contention with many producer and consumer threads. But being an "wait free" data structure, ConcurrentLinkedQueue will not block when empty, meaning that the consumer will need to deal with the poll() returning null values by "busy waiting", for example, with the consumer thread eating up CPU.

So which one is "better" depends on the number of consumer threads, on the rate they consume/produce, etc. A benchmark is needed for each scenario.

One particular use case where the ConcurrentLinkedQueue is clearly better is when producers first produce something and finish their job by placing the work in the queue and only after the consumers starts to consume, knowing that they will be done when queue is empty. (here is no concurrency between producer-consumer but only between producer-producer and consumer-consumer)

6 Comments

one doubt here. As you mentiond consumer waits when the queue is empty..how long does it wait. Who will notify it to not to wait?
@brindal The only way for it to wait, that I know of, is in a loop. Which is a significant problem that hasn't been given much attention in the answers here. Just running a loop waiting for data uses a lot of processor time. You'll know it when your fans start revving. The only remedy is to put a sleep in the loop. So it's a problem in a system with inconsistent data flow. Perhaps I misunderstand AlexandruNedelcu's answer, but an operating system itself is a concurrent system, that would be hugely inefficient if it were full of non-blocking event loops.
okay, but if unbounded blockingqueue is used would it be better than CAS based concurrent ConcurrentLinkedQueue
@orodbhen Putting a sleep would also not eliminate wastage. The OS has to do a lot of work to get a thread out of sleep and schedule and run it. If messages are not yet available, that work done by your OS goes waste. I would recommend it is better to use BlockingQueue, as it was specifically designed for producer-consumer problem.
actually, I'm very interested in the "consume/produce rate" part, so which one is better if rate goes high?
|
0

Another solution (that does not scale well) is rendezvous channels : java.util.concurrent SynchronousQueue

Comments

0

If your queue is non expandable and contains only one producer/consumer thread. You can use lockless queue (You don't need to lock the data access).

Comments

0

ConcurrentLinkedQueue.poll uses CAS to maintain thread safety.
LinkedBlockingQueue.poll uses ReentrantLock! So it is slower. But provides blocking functionality.
ConcurrentLinkedQueue is just a thread safe linked list.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.