Wednesday, October 16, 2013

LinkedBlockingDeque in java

LinkedBlockingDeque implements the BlockingQueue interface. Before moving further, please a look at the BlockingQueue interface that help you better understand LinkedBlockingDeque  and it's methods.



LinkedBlockingDeque 
Blocking means Attempts to put an element into a full LinkedBlockingDeque will result in the operation blocking;attempts to take an element from an empty LinkedBlockingDeque will similarly block. 
Deque means you can insert and remove the element from both the ends.
Linked means element are linked to each other and know who is in front and at the back.

  • Optionally-bounded blocking deque based on linked nodes.
  • Linked nodes are dynamically created upon each insertion unless this would bring the deque above capacity.
  • This class does not permit null elements.
  • Concurrent scalable optionally bounded FIFO blocking deque backed by linked nodes.

How to create LinkedBlockingDeque?
  • LinkedBlockingDeque() : Creates a LinkedBlockingDeque with a capacity of Integer.MAX_VALUE.
  • LinkedBlockingDeque(int capacity) : Creates a LinkedBlockingDeque with the given (fixed) capacity.
  • LinkedBlockingDeque(Collection<? extends E> c) : Creates a LinkedBlockingDeque with a capacity of Integer.MAX_VALUE, initially containing the elements of the given collection, added in traversal order of the collection's iterator.

There are few basic methods like add( E e ), elemet(), offer( E e ), peek() etc which are already discussed in Blocking interface. Here we discuss methods which is particularly  useful in case of LinkedBlockingDeque 


Modifier and Type Method  Description
void addFirst(E e) Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available.
void addLast(E e) Inserts the specified element at the end of this deque if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available
E getFirst() Retrieves, but does not remove, the first element of this deque.
E getLast() Retrieves, but does not remove, the last element of this deque.
boolean offerFirst(E e) Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available.
boolean offerLast(E e) Inserts the specified element at the end of this deque if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available.
E peekFirst() Retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty.
E peekLast() Retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty.
E pollFirst() Retrieves and removes the first element of this deque, or returns null if this deque is empty.
E pollLast() Retrieves and removes the last element of this deque, or returns null if this deque is empty.



Let see an example of usage of above methods.

Sample Output
[10, 43, 32]

Adding element at the front 45
[45, 10, 43, 32]

Adding elemet at the end 67
[45, 10, 43, 32, 67]

head of the deque/ the first element of this deque 
45
First element of deque
45
Last elementof deque
67

[45, 10, 43, 32, 67]
First element and remove it
45
[10, 43, 32, 67]

Last element and remove it
67
[10, 43, 32]

[10, 43, 32, 413, 31, 91, 13]

Size of deque : 7


Methods which are useful in case where multiple thread are use because they wait until some condition is met


Modifier and Type Method  Description
void put(E e) Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque), waiting if necessary for space to become available.
void putFirst(E e) Inserts the specified element at the front of this deque, waiting if necessary for space to become available.
void putLast(E e) Inserts the specified element at the end of this deque, waiting if necessary for space to become available.
E take() Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), waiting if necessary until an element becomes available.
E takeFirst() Retrieves and removes the first element of this deque, waiting if necessary until an element becomes available.
E takeLast() Retrieves and removes the last element of this deque, waiting if necessary until an element becomes available.


Producer Consumer problem with LinkedBlockingDeque 

Sample output
[0]:  consumed(0)  :[]
[]: inserted(0) :[]
[1]: inserted(1) :[1]
[1, 2]: inserted(2) :[1, 2]
[1, 2, 3]: inserted(3) :[1, 2, 3]
[1, 2, 3, 4]: inserted(4) :[1, 2, 3, 4]
[1, 2, 3, 4, 5]: inserted(5) :[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]:  consumed(1)  :[2, 3, 4, 5, 6]
[2, 3, 4, 5, 6]: inserted(6) :[2, 3, 4, 5, 6]
[2, 3, 4, 5, 6]:  consumed(2)  :[3, 4, 5, 6]
[3, 4, 5, 6, 7]: inserted(7) :[3, 4, 5, 6, 7]
[3, 4, 5, 6, 7]:  consumed(3)  :[4, 5, 6, 7]
[4, 5, 6, 7, 8]: inserted(8) :[4, 5, 6, 7, 8]
[4, 5, 6, 7, 8]:  consumed(4)  :[5, 6, 7, 8]
[5, 6, 7, 8, 9]: inserted(9) :[5, 6, 7, 8, 9]
[6, 7, 8, 9, 10]: inserted(10) :[6, 7, 8, 9, 10]

[5, 6, 7, 8, 9]:  consumed(5)  :[6, 7, 8, 9, 10]

Have you notice from the above output that element are consumed from the head of the deque and put() wait until consumer thread consume the element from deque.

Changed the method takeFirst() to takeLast(), try the above code. Sample output after changing the method name.

Sample Output
[0]: inserted(0) :[]
[0]:  consumed(0)  :[]
[1]: inserted(1) :[1]
[1, 2]: inserted(2) :[1, 2]
[1, 2, 3]: inserted(3) :[1, 2, 3]
[1, 2, 3, 4]: inserted(4) :[1, 2, 3, 4]
[1, 2, 3, 4, 5]: inserted(5) :[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]:  consumed(5)  :[1, 2, 3, 4]
[1, 2, 3, 4, 6]: inserted(6) :[1, 2, 3, 4, 6]
[1, 2, 3, 4, 7]: inserted(7) :[1, 2, 3, 4, 7]
[1, 2, 3, 4, 6]:  consumed(6)  :[1, 2, 3, 4, 7]
[1, 2, 3, 4, 7]:  consumed(7)  :[1, 2, 3, 4]
[1, 2, 3, 4, 8]: inserted(8) :[1, 2, 3, 4, 8]
[1, 2, 3, 4, 8]:  consumed(8)  :[1, 2, 3, 4]
[1, 2, 3, 4, 9]: inserted(9) :[1, 2, 3, 4, 9]
[1, 2, 3, 4, 10]: inserted(10) :[1, 2, 3, 4, 10]

[1, 2, 3, 4, 9]:  consumed(9)  :[1, 2, 3, 4, 10]


Related Post
PriorityQueue in java with examples
PriorityBlockingQueue in Java with examples
ArrayBlockingQueue in Java with Examples


If you know anyone who has started learning Java, why not help them out! Just share this post with them. 
Thanks for studying today!...

1 comment:

  1. I am technology Enthusiast. Your blog is really awesome, attractive and impressive. I like the way you think. it is very useful for Java SE & Java EE Learners. Your article adds best knowledge to our Java Online Training in India. or learn thru Java Online Training in India Students. or learn thru JavaScript Online Training in India. Appreciating the persistence you put into your blog and detailed information you provide. Kindly keep blogging.

    ReplyDelete