Tuesday, January 2, 2024

ConcurrentLinkedDeque in Java With Examples

ConcurrentLinkedDeque is another concurrent collection which is part of the java.util.concurrent package. Unlike many other concurrent collections like ConcurrentHashMap, CopyOnWriteArrayList which were added in Java 5 ConcurrentLinkedDeque was added in Java 7.

ConcurrentLinkedDeque in Java is an unbounded thread-safe Deque which stores its elements as linked nodes. Since it implements deque interface, ConcurrentLinkedDeque supports element insertion and removal at both ends. You will find methods like addFirst(), addLast(), getFirst(), getLast(), removeFirst(), removeLast() to facilitate operations at both ends.

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

Usage of ConcurrentLinkedDeque

A ConcurrentLinkedDeque in Java is an appropriate choice when many threads share access to a common collection as concurrent insertion, removal, and access operations execute safely across multiple threads.

Note that it doesn't block operations as done in the implementation of BlockingDequeue interface like LinkedBlockingDeque. So there are no putFirst(), takeFirst() or putLast(), takeLast() methods which will wait if required.

Java ConcurrentLinkedDeque Iterator

ConcurrentLinkedDeque's iterator is weakly consistent, returning elements reflecting the state of the queue at some point at or since the creation of the iterator. Iterators in ConcurrentLinkedDeque do not throw ConcurrentModificationException, and may proceed concurrently with other operations. Elements contained in the queue since the creation of the iterator will be returned exactly once.

ConcurrentLinkedDeque Java Example

Let's create a producer consumer using ConcurrentLinkedDeque. In this code there will be one producer thread putting element into the queue and two consumer threads retrieving elements from the queue. Note that producer thread will put 5 elements.

import java.util.Deque;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ConcurrentLDeQDemo {
  public static void main(String[] args) {
    //Buffer buffer = new Buffer();
    Deque<Integer> clDQue = new ConcurrentLinkedDeque<Integer>();
    ExecutorService executor = Executors.newFixedThreadPool(3);
    // Calling one producer
    executor.execute(new ProdTask(clDQue));
    // Calling two consumers
    executor.execute(new ConTask(clDQue));
    executor.execute(new ConTask(clDQue));
    executor.shutdown();
  }
}

/**
 * 
 */
class ProdTask implements Runnable{
  // Shared Deque object
  Deque<Integer> clDQue;
  ProdTask(Deque<Integer> clDQue){
    this.clDQue = clDQue;
  }
  @Override
  public void run() {
    for(int i = 0; i < 5; i++){
      clDQue.add(i);
    }
  }
}

/**
 * 
 */
class ConTask implements Runnable{
  Integer value;
  // Shared Deque object
  Deque<Integer> clDQue;
  ConTask(Deque<Integer> clDQue){
    this.clDQue = clDQue;
  }
  @Override
  public void run() {
    while ((value = clDQue.pollFirst()) != null) {
      if(value == null){
        System.out.println("No value to poll");
      }else{
        System.out.println("Consumer recd - " + value);
      }
    }
  }    
}

Output

Consumer recd - 0
Consumer recd - 1
Consumer recd - 2
Consumer recd - 3
Consumer recd - 4

That's all for this topic ConcurrentLinkedDeque in Java With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. ConcurrentLinkedQueue in Java With Examples
  2. Java BlockingQueue With Examples
  3. Java BlockingDeque With Examples
  4. ConcurrentHashMap in Java With Examples
  5. Java Concurrency Interview Questions And Answers

You may also like-

  1. Race Condition in Java Multi-Threading
  2. Lambda Expressions in Java 8
  3. @FunctionalInterface Annotation in Java
  4. Interface Static Methods in Java
  5. How HashMap Works Internally in Java
  6. Fail-Fast Vs Fail-Safe Iterator in Java
  7. Multi-Catch Statement in Java Exception Handling
  8. Print Odd-Even Numbers Using Threads And wait-notify Java Program