Friday, 3 June 2016

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 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.

Usage of ConcurrentLinkedDeque

A ConcurrentLinkedDeque is an appropriate choice when many threads will 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.

No nulls

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

Iterator

Iterators are weakly consistent, returning elements reflecting the state of the queue at some point at or since the creation of the iterator. They 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.

Example Code

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. LinkedBlockingDeque in Java
  5. ArrayBlockingQueue in Java Concurrency
  6. Java Concurrency interview questions

You may also like -

No comments:

Post a Comment