Comprehensive Guide to the Java Queue Family: 18 Implementations, Interfaces, Core Methods and Usage Patterns
This article provides a thorough, English‑language overview of Java's Queue hierarchy, detailing the 18 concrete queue classes, their inheritance relationships, core methods, blocking variants, deque and transfer semantics, and practical code examples for each implementation.
1. Introduction to Java Queues
The article begins by introducing the concept of a queue ( Queue ) as a FIFO data structure and mentions its two close relatives, List and Set , all belonging to the java.util package.
2. Classification of the 18 Queue Types
All 18 queue implementations are grouped into three categories: interfaces, abstract classes, and concrete classes. A diagram shows the inheritance chain, e.g., Queue extends Collection , which extends Iterable .
2.1 Core Interfaces
Queue defines three groups of operations (insert, remove, examine) each with a method that throws an exception and a method that returns a special value (null or false). The table illustrates add / offer , remove / poll , and element / peek .
2.2 Abstract Classes
AbstractQueue provides skeletal implementations of add , remove and element by delegating to offer , poll and peek . Example code is shown:
public boolean add(E e) {
if (offer(e))
return true;
else
throw new IllegalStateException("Queue full");
}
public E remove() {
E x = poll();
if (x != null)
return x;
else
throw new NoSuchElementException();
}
public E element() {
E x = peek();
if (x != null)
return x;
else
throw new NoSuchElementException();
}3. Blocking Queues
BlockingQueue adds blocking put and take operations. When the queue is full, producers block; when empty, consumers block. Typical use‑case: producer‑consumer pipelines.
3.1 Core Methods
Four insertion methods ( add , offer , put , timed offer ), four removal methods ( remove , poll , take , timed poll ) and two inspection methods ( element , peek ) are described, together with the exceptions they may throw.
3.2 Implementations
ArrayBlockingQueue – bounded array‑backed queue (optional fairness).
LinkedBlockingQueue – optionally bounded linked‑list queue, default capacity Integer.MAX_VALUE .
LinkedBlockingDeque – double‑ended version of LinkedBlockingQueue .
PriorityBlockingQueue – priority‑ordered blocking queue.
DelayQueue – holds elements that implement Delayed and become available after a timeout.
SynchronousQueue – hand‑off queue with no internal capacity.
4. Deque (Double‑Ended Queue)
The Deque interface extends Queue and adds methods for inserting and removing at both ends ( addFirst , addLast , removeFirst , removeLast , etc.). It can be used as a stack (LIFO) or a regular queue (FIFO). Implementations include LinkedList , ArrayDeque , ConcurrentLinkedDeque , and LinkedBlockingDeque .
5. TransferQueue
TransferQueue adds a “mission‑critical” transfer operation: transfer(E e) blocks until a consumer receives the element. Non‑blocking variants tryTransfer and timed tryTransfer are also provided. Implementations: LinkedTransferQueue .
6. Priority Queues
PriorityQueue (non‑blocking) and PriorityBlockingQueue (blocking) order elements according to their natural ordering or a supplied Comparator . They do not permit null elements and have O(log n) insertion/removal.
7. Concrete Collection Classes
LinkedList – implements List , Deque , Queue ; not thread‑safe.
ArrayDeque – array‑backed, non‑blocking, not thread‑safe.
ConcurrentLinkedQueue – lock‑free, thread‑safe, non‑blocking FIFO.
ConcurrentLinkedDeque – lock‑free, thread‑safe double‑ended queue.
SynchronousQueue – hand‑off queue with no storage.
8. Usage Examples
Code snippets demonstrate adding elements to a LinkedBlockingDeque ( addFirst , addLast ), using ConcurrentLinkedQueue with custom objects, and a producer‑consumer scenario with LinkedTransferQueue where transfer blocks until a consumer calls take .
9. Practical Recommendations
When to choose each implementation is discussed: use ArrayDeque for fast stack/queue operations, LinkedList when frequent insertions/removals at both ends are needed, ConcurrentLinkedQueue for high‑throughput multi‑threaded pipelines, and SynchronousQueue for hand‑off patterns such as thread‑pool task queues.
10. Conclusion
The article aggregates official documentation, diagrams, and runnable examples to deliver what the author claims is the most complete and detailed explanation of Java's queue family.
Wukong Talks Architecture
Explaining distributed systems and architecture through stories. Author of the "JVM Performance Tuning in Practice" column, open-source author of "Spring Cloud in Practice PassJava", and independently developed a PMP practice quiz mini-program.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.