Useful when order matters
Queue has a head of the queue where elements are taken off. Elements are added to the tail of the queue. It has methods that read from the head but do not remove the element. Other methods read and remove from the head. Some methods throw an exception if there is nothing to read; some just return null.
Has methods like:
boolean add( E e ) - add to end of queue - returns true if could add (some queues have max size) or throws IllegalStateException
boolean offer(E e ) - add to end of queue - returns true if could add (some queues have max size) or false.
E poll() - Retrieves and removes the head of this queue. Returns null if there are no more elements.
E element() - Retrieves but does not remove. Throws NoSuchElementException if there is no element
E peek() - Retrieves but does not remove. Returns null if no element.
Use LinkedBlockingQueue when:
you do not require blocking based on size. LinkedBlockingQueue has optional blocking on size. (ArrayBlockingQueue blocks on size)
you do not require fairness (ArrayBlockingQueue has fairness)
there is very high contention (ConcurrentLinkedQueue works better with medium contention. With high contention ConcurrentLinkedQueue wastes cycles on failed CAS attempts)
don't care so much about erratic garbage collection (ArrayBlockingQueue has more uniform garbage collection)
Use ArrayBlockingQueue when:
you require blocking (ConcurrentLinkedQueue does not have blocking)
you require fairness (LinkedBlockingQueue does not have fairness)
you require consistent performance (garbage collection is more uniform than using LinkedBlockingQueue).
you don't require blocking on size (ArrayBlockingQueue has blocking on size and LinkedBlockingQueue optionally has blocking)
there is not a real lot of contention (CAS operations are much faster than locking if there is not a massive amount of contention)
Use LinkedBlockingDeque when:
you require thread safe behaviour (else ArrayDeque is better)
you require blocking behaviour (else ConcurrentLinkedDeque is better)
there is very high contention (else use ConcurrentLinkedDeque is better as CAS operations more efficient when there is not very high contention)
you need methods for both sides of the queue
Performance:
O(1) Constant Time: Most operations run in constant time (ignoring time spent blocking).
O(n) Linear Time: Exceptions include remove, removeFirstOccurrence, removeLastOccurrence, contains, iterator.remove(), and the bulk operations, all of which run in linear time.
you do not require thread safe behaviour (else use ConcurrentLinkedDeque or LinkedBlockingDeque)
need methods for both sides of the queue
Use ConcurrentLinkedDeque when:
you do not require blocking behaviour (else use LinkedBlockingDeque)
contention is not very high (else use LinkedBlockingDeque)
you need methods for both sides of the queue
Use DelayQueue when:
you are storing objects which implement Delayed.
Characteristics
Delayed has a method, getDelay(TimeUnit unit), which returns the number of units for the TimeUnit until expiry. If the object has already expired the delay will be zero or a minus value. Values which are the most minus are at the head of the queue
Use PriorityQueue when:
you wish to sort the elements in the queue by a certain priority.
you are using a single threaded environment
Characteristics
To sort by a priority you need to create a comparator and pass it to the constructor.
Use PriorityBlockingQueue when:
you wish to sort the elements in the queue by a certain priority
you are using a multi threaded environment
Characteristics
blocks on take if there are no entries but does not block on put
even though it has blocking in the name it is actually unbounded
Use SynchronousQueue when:
you want one thread to put an element in the queue and block until another thread takes the element.
you do not require BlockingQueue methods (Synchronous Queue is not a real queue. If you want similar functionality but also require BlockingQueue methods then use LinkedTransferQueue)
Use LinkedTransferQueue when:
you want to have use of BlockingQueue methods
you also want to have the option to use the new TrasferQueue transfer method to block until the consumer receives the element (similar to SynchronousQueue)
Back: Data Structures
Page Author: JD