June 28, 2022

ConcurrentLinkedDeque in Java With Examples

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. ConcurrentLinkedDeque in Java implements Deque interface and is part of java.util.concurrent package.

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.

public class ConcurrentLDQ {
  public static void main(String[] args) {
    Deque<Integer> conDeque = new ConcurrentLinkedDeque<>();
    conDeque.add(10);
    conDeque.addLast(null);
  }
}
Output
Exception in thread "main" java.lang.NullPointerException
	at java.base/java.util.Objects.requireNonNull(Objects.java:221)
	at java.base/java.util.concurrent.ConcurrentLinkedDeque.linkLast(ConcurrentLinkedDeque.java:347)
	at java.base/java.util.concurrent.ConcurrentLinkedDeque.addLast(ConcurrentLinkedDeque.java:840)
	at com.knpcode.programs.ConcurrentLDQ.main(ConcurrentLDQ.java:11)

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.

public class ConcurrentLDQ {
  public static void main(String[] args) {
    ExecutorService executor = Executors.newFixedThreadPool(4);
    Deque<Integer> conDeque = new ConcurrentLinkedDeque<>();
    // One Producer thread
    executor.execute(new ConProducer(conDeque));
    // Two Consumer thread
    executor.execute(new ConConsumer(conDeque));
    executor.execute(new ConConsumer(conDeque));    
    executor.shutdown();
  }
}

//Producer
class ConProducer implements Runnable{
  Deque<Integer> conDeque;
  ConProducer(Deque<Integer> conDeque){
    this.conDeque = conDeque;
  }
  @Override
  public void run() {
    for(int i = 0; i < 6; i++){
      System.out.println("Adding to queue-" + i);
      conDeque.addFirst(i);    
    }
  }
}

//Consumer
class ConConsumer implements Runnable{
  Deque<Integer> conDeque;
  ConConsumer(Deque<Integer> conDeque){
    this.conDeque = conDeque;
  }
  @Override
  public void run() {        
    for(int i = 0; i < 3; i++){
      try {
        TimeUnit.MILLISECONDS.sleep(10);                
        System.out.println("Thread Name -" + Thread.currentThread().getName() + 
            " Consumer retrieved- " + conDeque.pollLast());
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }
}
Output
Adding to queue-0
Adding to queue-1
Adding to queue-2
Adding to queue-3
Adding to queue-4
Adding to queue-5
Thread Name -pool-1-thread-3 Consumer retrieved- 0
Thread Name -pool-1-thread-2 Consumer retrieved- 1
Thread Name -pool-1-thread-3 Consumer retrieved- 2
Thread Name -pool-1-thread-2 Consumer retrieved- 3
Thread Name -pool-1-thread-3 Consumer retrieved- 4
Thread Name -pool-1-thread-2 Consumer retrieved- 5

That's all for the topic ConcurrentLinkedDeque in Java With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment