Blocking Queues
Queues (and deques) are the indisputable choice when choosing a collection to manage shared data in producer-consumer problems. The interfaces for thread-unsafe queues (Queue, Deque) in the java.util package have been enhanced in the java.util.concurrent package to provide a wide variety of blocking queues that are thread-safe (Figure 23.6). These thread-safe queues are blocking because a thread blocks when trying to add an element and the queue is full, or when trying to remove an element and the queue is empty.

Figure 23.6 Blocking Queues in the java.util.concurrent Package
A summary of interfaces and classes that implement blocking queues and deques provided by the java.util.concurrent package is given in Table 23.11. Salient characteristics of these queues and deques are summarized in Table 23.12. None of the queues or deques allow a null value for an element, but they do allow duplicates. Being queues, FIFO ordering is the norm and a majority of them are weakly consistent when it comes to serial access.
Table 23.11 Blocking Queues in the java.util.concurrent Package
Blocking queues | Description |
interface BlockingQueue<E> extends Queue<E> (Table 23.13) | A Queue that additionally supports operations that wait for the queue to become non-empty when retrieving an element, and wait for space to become available in the queue when storing an element. |
LinkedBlockingQueue<E> implements BlockingQueue<E> | An optionally bounded blocking queue based on linked nodes. |
ArrayBlockingQueue<E> implements BlockingQueue<E> | A bounded blocking queue backed by an array. A fairness policy can be specified so that threads blocked for insertion and removal are processed in FIFO order. |
PriorityBlockingQueue<E> implements BlockingQueue<E> | An unbounded blocking queue that uses the same ordering rules as class PriorityQueue and supplies blocking retrieval operations. Implementation is based on a binary heap. |
SynchronousQueue<E> implements BlockingQueue<E> | A blocking queue in which each insert operation must wait for a corresponding remove operation by another thread, and vice versa. No elements are stored. |
DelayQueue<E extends Delayed> implements BlockingQueue<E> | An unbounded blocking queue of Delayed elements, in which an element can only be taken when its delay has expired. Elements implement the java.util.concurrent.Delay interface. Whether an element is available or not, it still counts toward the size of the queue. Implementation is based on a PriorityQueue instance that in turn uses a binary heap. |
interface TransferQueue<E> extends BlockingQueue<E> | A BlockingQueue in which producers may wait for consumers to receive elements. |
LinkedTransferQueue<E> implements TranferQueue<E> | An unbounded blocking TransferQueue based on linked nodes. The elements in the queue are ordered in FIFO order with respect to a given producer waiting for consumers. |
interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> (Table 23.14) | A Deque that additionally supports blocking operations that wait for the deque to become nonempty when retrieving an element, and wait for space to become available in the deque when storing an element. |
LinkedBlockingDeque<E> implements BlockingDeque<E> | An optionally bounded blocking deque based on doubly linked nodes. |
Table 23.12 Characteristics of Blocking Queues
Blocking queues | null value | Duplicates | Ordering | Kind of iterator |
LinkedBlockingQueue<E> implements BlockingQueue<E> | No | Allowed | FIFO order | Weakly consistent |
ArrayBlockingQueue<E> implements BlockingQueue<E> | No | Allowed | FIFO order | Weakly consistent |
PriorityBlockingQueue<E> implements BlockingQueue<E> | No | Allowed | Natural ordering | Fail-fast |
SynchronousQueue<E> implements BlockingQueue<E> | No | Not applicable | Not applicable | Not applicable |
DelayQueue<E extends Delayed> implements BlockingQueue<E> | No | Allowed | Longest-delayed order | Fail-fast |
LinkedTransferQueue<E> implements TranferQueue<E> | No | Allowed | FIFO order | Weakly consistent |
LinkedBlockingDeque<E> implements BlockingDeque<E> | No | Allowed | FIFO/LIFO order | Weakly consistent |