Tuesday, December 10, 2019

ConcurrentLinkedDeque in Java

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. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. ConcurrentLinkedQueue in Java
  2. BlockingQueue in Java Concurrency
  3. BlockingDeque in Java Concurrency
  4. ConcurrentHashMap in Java
  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. Functional interface annotation in Java 8
  4. interface static methods in Java 8
  5. How HashMap internally works in Java
  6. fail-fast Vs fail-safe iterator in Java
  7. Multi catch statement in Java 7
  8. Print odd-even numbers using threads and wait-notify