Concurrent collections question 71 of 180 mid
ArrayBlockingQueue or LinkedBlockingQueue, and does bounded matter?
LinkedBlockingQueueis always faster because it has two locks- Correct: Bounded is the important decision: an unbounded queue turns a slow consumer into an OutOfMemoryError instead of backpressure
- They are interchangeable
ArrayBlockingQueueis unbounded by default
The two-lock design of LinkedBlockingQueue (separate put and take locks) is often quoted as faster,
and measured in the sibling repository it was not: ArrayBlockingQueue won both single threaded (23.6
against 11.1 ops/us) and with four producers against four consumers (49.5 against 40.9). It reuses a
ring buffer instead of allocating a node per element.
The choice that actually matters is the bound. Unbounded means a producer that outruns its consumer fills the heap until the process dies, usually at the worst moment. Bounded means the producer blocks, the pressure propagates upstream, and the system degrades instead of falling over.
Know the rest of the family too: SynchronousQueue has no capacity at all and hands off directly,
PriorityBlockingQueue orders by comparator and is unbounded, DelayQueue releases elements only when
their delay expires, and LinkedTransferQueue lets a producer wait until its element is actually
taken.
Quiz yourself on Concurrent collections Suggest a correction