The Queue implementations in Java are grouped into general purpose (implementations of of java.util.Queue interface), and concurrent (implementations of java.util.concurrent.BlockingQueue interface)
java.util.Queue interface
- Designed for holding elements prior to processing in a single-threaded environment. All operations are not thread-safe
- Generally do not allow insertion of
null
elements - Supported since Java 1.5
Supported Operations
java.util.Queue
methods come in 2 forms
Throw an exception if the operation fails
add(e)
, equivalent toenqueue
, return true upon success and throwingIllegalStateException
if no space is currently availableremove
, equivalent todequeue
, throwingNoSuchElementException
if the queue is emptyelement
, equivalent tofront
, throwingNoSuchElementException
if the queue is empty
Returns the special value (either
null
orfalse
) depending on the operation, preferred for capacity restricted queueoffer(e)
, equivalent toenqueue
, return true upon success and false if no space is currently availablepoll
, equivalent todequeue
, returnnull
if the queue is emptypeek
, equivalent tofront
, returnnull
if the queue is empty
Implementations
LinkedList
, an optionally bounded FIFO queue backed by linked nodesPriorityQueue
, an unbounded priority queue backed by a heap
Usage Example
Queue<Integer> queue = new LinkedList<>();
// Group of operations returning special value (either `null` or `false`)
// if the operation fails
queue.offer(1);
queue.peek();
queue.poll();
// Group of operations throwing an exception
// if the operation fails
queue.add(2);
queue.element();
queue.remove();
java.util.concurrent.BlockingQueue interface
- Designed to be used primarily for producer-consumer queues in a multi-threaded environment. All public operations are thread-safe
- Does not accept null elements, throw
NullPointerException
on attempts to add, put or offer anull
. - Supported since Java 1.5
Supported Operations
java.util.concurrent.BlockingQueue
methods come in 4 forms
Throw an exception if the operation fails, extended from
java.util.Queue
(add(e)
,remove
andelement
)Returns special value (either
null
orfalse
) depending on the operation, extended fromjava.util.Queue
(offer(e)
,poll
andpeek
)Blocks the current thread indefinitely until the operation can succeed
put(e)
, equivalent toenqueue
, waiting if necessary for space to become availabletake()
, equivalent todequeue
, waiting if necessary until an element becomes available
Blocks for only a given maximum time limit before giving up
offer(e, time, unit)
, equivalent toenqueue
, waiting up to the specified wait time if necessary for space to become available.poll(time, unit)
, equivalent todequeue
, waiting up to the specified wait time if necessary for an element to become available.
Implementations
ArrayBlockingQueue
, a bounded FIFO blocking queue backed by an arrayLinkedBlockingQueue
, an optionally bounded FIFO blocking queue backed by linked nodesPriorityBlockingQueue
, an unbounded blocking priority queue backed by a heap
Usage Example