Java BlockingDeque Example
November 17, 2023
1. Java BlockingDeque is introduced in Java 6 and is the part of collection framework. BlockingDeque
is a deque collection which supports blocking operation.
2. Deque is a queue that supports insertion and deletion at both sides.
3. In
BlockingDeque
, while insertion it waits to become empty if already full and while deletion it waits to become non-empty if already empty.
add() vs offer() vs put()
The main feature ofBlockingDeque
is to wait while inserting or deleting element if necessary.
1.
put()
guarantees to add element in BlockingDeque
, because if there is no space then it will wait to become empty.
2.
add()
will not wait if there is no space and will throw IllegalStateException
.
3.
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.
peek() vs poll() vs remove() vs take() vs element()
peek(): fetches the element from the head of the queue but does not remove fromBlockingDeque
.
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.
Using BlockingDeque
BlockingDeque<E> bd = new LinkedBlockingDeque<E>();
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(); } } } } }
BlockingDeque
is 1, so adding and fetching process 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