Deque

Programming is like sex. One mistake and you have to support it for the rest of your life. Michael Sinz

LinkedBlockingDeque

Language Java | Level Intermediate | Category Java collections framework (JCF) | 2015-07-22 19:02:54

0 Comments


Definition

The LinkedBlockingDeque is a double-ended queue which will block if a thread attempts to take elements out of it while it is empty. Most operations run in constant time.

Character

The capacity takes from the constructor. If we do not specify, it will take Integer.MAX_VALUE.
It derives from collection framework.
drainTo method removes all available elements from this queue and adds them to the given collection.
LinkedBlockingDeque class is intended to be the "standard" blocking deque class.

source Code

								
package com.careerdrill.collections.deque;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

class Producer implements Runnable {
	  private String name;

	  private BlockingDeque< Integer > deque;

	  public Producer(String name, BlockingDeque< Integer > deque) {
	    this.name = name;
	    this.deque = deque;
	  }

	  public synchronized void run() {

	    for (int i = 0; i < 10; i++) {
	      try {
	        deque.putFirst(i);
	        System.out.println(name + " puts " + i);
	        Thread.sleep(300);

	      } catch (InterruptedException e) {
	        e.printStackTrace();
	      }
	    }

	  }
	}

	class Consumer implements Runnable {
	  private String name;

	  private BlockingDeque< Integer > deque;

	  public Consumer(String name, BlockingDeque< Integer > deque) {
	    this.name = name;
	    this.deque = deque;
	  }

	  public synchronized void run() {
	    for (int i = 0; i < 10; i++) {
	      try {
	        int j = deque.takeLast();
	        System.out.println(name + " takes " + j);
	        Thread.sleep(3000);
	      } catch (InterruptedException e) {
	        e.printStackTrace();
	      }
	    }
	  }
	}

	
public class LinkedBlockingDequeUse {

	public static void main(String[] args) {
		BlockingDeque< Integer > deque = new LinkedBlockingDeque< Integer >(5);
	    Runnable producer = new Producer("Producer", deque);
	    Runnable consumer = new Consumer("Consumer", deque);
	    new Thread(producer).start();
	    try {
	      Thread.sleep(500);
	    } catch (InterruptedException e) {
	      e.printStackTrace();
	    }

	    new Thread(consumer).start();

	}

}
						

Output

    					 
Producer puts 0
Producer puts 1
Consumer takes 0
Producer puts 2
Producer puts 3
Producer puts 4
Producer puts 5
Consumer takes 1
Producer puts 6
Consumer takes 2
Producer puts 7
Consumer takes 3
Producer puts 8
Consumer takes 4
Producer puts 9
Consumer takes 5
Consumer takes 6
Consumer takes 7
Consumer takes 8
Consumer takes 9
    					 
					    

Comparison

LinkedBlockingDeque ConcurrentLinkedDeque
The LinkedBlockingDeque class is intended to be the "standard" blocking deque class. ConcurrentLinkedDeque has almost the opposite performance profile as LinkedBlockingDeque.
Poor scalability good scalability
LinkedBlockingQueue uses locking. ConcurentLinkedDequeue is lock-free
The size returns the current size of linked blocking dequeue. the size method is NOT a constant-time operation.

When to use

When the application needs to develop thread-safe manner with locking, use LinkedBlockingDeque. When the application needs access the data from multiple threads, use ConcurrentLinkedDeque

0 Comments



Please login to add comments.