ConcurrentLinkedDeque in Java

ConcurrentLinkedDeque in Java implements Deque interface and is part of java.util.concurrent package.

ConcurrentLinkedDeque in Java is an unbounded concurrent deque which is thread-safe. It stores its elements as linked nodes where each node stores a reference to both previous and next nodes.

How ConcurrentLinkedDeque differs from the blocking Deque implementations like LinkedBlockingDeque is that ConcurrentLinkedDeque is non-blocking so the operations in this queue don’t block. Since ConcurrentLinkedDeque is non-blocking so the blocking methods like putFirst(), takeFirst() or putLast(), takeLast() which will block if required are not available.

ConcurrentLinkedDeque is similar to its counterpart ConcurrentLinkedQueue, with one difference that it is a double ended queue. It means ConcurrentLinkedDeque allows insertion and removal from both ends. ConcurrentLinkedDeque has methods like addFirst(), addLast(), removeFirst(), removeLast() to facilitate insertion and removal from both ends.

ConcurrentLinkedDeque doesn’t allow null elements

Like most other concurrent collection implementations, this class does not permit the use of null elements.

Output

As you can see trying to add null to the deque results in NullPointerException.

Java ConcurrentLinkedDeque constructors

  • ConcurrentLinkedDeque()– Constructs an empty deque.
  • ConcurrentLinkedDeque(Collection<? extends E> c)– Constructs a deque initially containing the elements of the given collection, added in traversal order of the collection’s iterator.

ConcurrentLinkedDeque Java example

Here is an example of producer-consumer in Java using ConcurrentLinkedDeque. There is one producer thread and two consumers threads.

Output

That’s all for the topic ConcurrentLinkedDeque 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.