Home  >  Core Java

Example of BlockingDeque in Java

By Arvind Rai, December 29, 2013
java.util.concurrent.BlockingDeque has been introduced in JDK 6 and is the part of collection framework. BlockingDeque is a deque collection which supports blocking operation. Deque is a queue that supports insertion and deletion at both sides. BlockingDeque means while insertion, it waits to become empty if already full and while deletion it waits to become non-empty if already empty. There is some interesting method description of BlockingDeque.

Difference among add(), offer() and put() of BlockingDeque in Java

The main feature of BlockingDeque is to wait while inserting or deleting element if necessary. put() guarantees to add element in BlockingDeque, because if there is no space then put() will wait to become empty. add() will not wait if no space and throw IllegalStateException. offer() can be used when we do not want to wait and also do not want to get exception if no space. So offer() simply returns false if got no space immediately or returns true if success.

Difference among peek(), poll(), remove(), take(), element() of BlockingDeque in Java

peek() : fetches the element from the head of the queue but does not remove from BlockingDeque.
poll() : fetches the element from the head of the queue and remove also from BlockingDeque, returns null if empty.
remove() : fetches the element from the head of the queue and remove also from BlockingDeque, throws NoSuchElementException if empty.
take() : fetches the element from the head of the queue and remove also from BlockingDeque, waits if empty.
element() : fetches the element from the head of the queue but does not remove from BlockingDeque, throws NoSuchElementException if empty.

How to Use BlockingDeque in Java

BlockingDeque is an interface. It has been implemented by java.util.concurrent.LinkedBlockingDeque.
BlockingDeque<E> lbd=new LinkedBlockingDeque<E>();
 
Find the example below.
BlockingDequeDemo.java
package com.concretepage.concurrent;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
public class BlockingDequeDemo {
	static BlockingDeque<String> bd=new LinkedBlockingDeque<String>(1);
	public static void main(String[] args) {
		ExecutorService exService = Executors.newFixedThreadPool(2);
		BlockingDequeDemo bdeque = new BlockingDequeDemo();
		ElementAdd elementAdd = bdeque.new ElementAdd();
		ElementGet elementGet = bdeque.new ElementGet();
		exService.execute(elementAdd);
		exService.execute(elementGet);
		exService.shutdown();
	}
	class ElementAdd implements Runnable{
		@Override
		public void run() {
			for(;;){
				try {
					String s= bd.take();
					System.out.println("Element received is: "+s);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
	class ElementGet implements Runnable{
		@Override
		public void run() {
			for(int i=0;i < 5;i++){
				try {
					bd.put("A"+i);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
} 
In the example, there is two threads, one is trying to add element and second is fetching element. The size of BlockingDeque is one, so adding and fetching has two wait if necessary. Because take() method waits if no element and put method also waits if no space to add.
Element received is: A0
Element received is: A1
Element received is: A2
Element received is: A3
Element received is: A4
 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us