views:

296

answers:

3

I need a queue that can be processed by multiple readers.

The readers will dequeue an element and send it to a REST service.

What's important to note are:

  • Each reader should be dequeueing different elements. If the queue has elements A, B & C, Thread 1 should dequeue A and Thread 2 should dequeue B in concurrent fashion. And so forth until there's nothing in the queue.
  • I understand that it is CPU intensive to always run in busy loop, peeking into the queue for items. So I am not sure if a Blocking queue is a good option.

What are my options ? Thanks.

+2  A: 

I am not sure from your question description whether the threads need to dequeue elements in a strict round-robin fashion. Assuming this isn't a restriction you can use BlockingQueue's take() method, which will cause the thread to block until data is available (therefore not consuming CPU cycles).

Also note that take() implementations are atomic (e.g. LinkedBlockingQueue): If multiple threads are blocked on take() and a single element is enqueued then only one thread's take() call will return; the other will remain blocked.

Adamski
No there's no need for the round-robin fashion. I might even bump up the reader thread count to 3 or 4. Sounds like LinkedBlockingQueue is a good fit for me. Thanks.
Jacques René Mesrine
+3  A: 

ConcurrentLinkedQueue or LinkedBlockingQueue are two options that immediately come to mind, depending on whether you want blocking behavior or not.

As Adamski notes, the take() method of the LinkedBlockingQueue does not needlessly burn cpu cycles while waiting for data to arrive.

Amber
Actually I'm a little unclear as to which Queue implementation is more appropriate: LinkedBlockingQueue or ConcurrentLinkedQueue - I always use LinkedBlockingQueue out of habit. Anyone got any recommendations here?
Adamski
ConcurrentLinkedQueue does not have any blocking, so is most likely not appropriate for your use case. the standard producer/consumer collection is a BlockingQueue. LinkedBlockingQueue is a good default choice.
james
Agreed with james - LinkedBlockingQueue using `take()` is probably the appropriate solution for this case.
Amber
A: 

The major difference between ConcurrentLinkedQueue and LinkedBLockingQueue is its throughput. Under moderate thread contention ConcurrentLinkedQueue greatly out performs all other BlockingQueues. Under heavy contetion, however, a BlockingQueue is a slightly better choice as it will appropriately put contending threads into the waiting thread set.

John W