LinkedBlockingDeque in Java

LinkedBlockingDeque in Java is an implementation of BlockingDeque interface and is part of java.util.concurrent package. LinkedBlockingDeque, just like LinkedBlockingQueue, is an optionally-bounded blocking queue which means the capacity for the queue can be specified thus making it bounded or it can be left unspecified in which case capacity of the deque is Integer.MAX_VALUE.

LinkedBlockingDeque in Java

LinkedBlockingDeque is based on linked nodes where each node holds reference to both previous and next nodes. Linked nodes are dynamically created upon each insertion unless this would bring the deque above capacity.

Since LinkedBlockingDeque is a deque (double ended queue) so elements can be inserted and removed from both ends of the queue that is how LinkedBlockingDeque is different from the BlockingQueue implementations like ArrayBlockingQueue where elements are added to the end of the queue and retrieved from the head of the queue.

LinkedBlockingDeque implementation is thread-safe. All queuing methods in the class achieve their effects atomically using ReentrantLock internally.

Java LinkedBlockingDeque constructors

  • LinkedBlockingDeque()– When no capacity is specified LinkedBlockingDeque is created with a capacity of Integer.MAX_VALUE.
  • LinkedBlockingDeque(int capacity)– Creates a LinkedBlockingDeque with the given (fixed) capacity.
  • LinkedBlockingDeque(Collection<? extends E> c)– Creates a LinkedBlockingDeque with a capacity of Integer.MAX_VALUE, initially containing the elements of the given collection, added in traversal order of the collection’s iterator.

LinkedBlockingDeque does not allow null elements

Implementations of BlockingDeque does not allow null elements.

Output

LinkedBlockingDeque Java example

Here is a producer-consumer example using the LinkedBlockingDeque. Each producer thread put 5 integer values in the queue where as consumer thread retrieves those values from the queue. Thread pool is only of two threads and the consumer blocks if consumer is executed first, for that takeLast() method is used.

Output

LinkedBlockingDeque class methods

In BlockingDeque implementation like LinkedBlockingDeque methods for adding and removing an element come in four forms-
One throws an exception, the second returns a special value (either null or false, depending on the operation), the third blocks the current thread indefinitely until the operation can succeed, and the fourth blocks for only a given maximum time limit before giving up.

Insertion methods

  • addFirst(E e)– Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available.
  • addLast(E e)– Inserts the specified element at the end of this deque if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available.
  • offerFirst(E e)– Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available.
  • offerLast(E e)– Inserts the specified element at the end of this deque if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available.
  • putFirst(E e)– Inserts the specified element at the front of this deque, waiting if necessary for space to become available.
  • putLast(E e)– Inserts the specified element at the end of this deque, waiting if necessary for space to become available.
  • offerFirst(E e, long timeout, TimeUnit unit)– Inserts the specified element at the front of this deque, waiting up to the specified wait time if necessary for space to become available.
  • offerLast(E e, long timeout, TimeUnit unit)– Inserts the specified element at the end of this deque, waiting up to the specified wait time if necessary for space to become available.

Removal methods

  • removeFirst()– Retrieves and removes the first element of this deque. Throws an exception if this deque is empty.
  • removeLast()– Retrieves and removes the last element of this deque. Throws an exception if this deque is empty.
  • pollFirst()– Retrieves and removes the first element of this deque, or returns null if this deque is empty.
  • pollLast()– Retrieves and removes the last element of this deque, or returns null if this deque is empty.
  • takeFirst()– Retrieves and removes the first element of this deque, waiting if necessary until an element becomes available.
  • takeLast()– Retrieves and removes the last element of this deque, waiting if necessary until an element becomes available.
  • pollFirst(long timeout, TimeUnit unit)– Retrieves and removes the first element of this deque, waiting up to the specified wait time if necessary for an element to become available.
  • pollLast(long timeout, TimeUnit unit)– Retrieves and removes the last element of this deque, waiting up to the specified wait time if necessary for an element to become available.

That’s all for the topic LinkedBlockingDeque in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.