April 17, 2017

All about BlockingQueue!

Concurrent Utility classes like BlockingQueue, ConcurrentHashMap, Counting Semaphore, CopyOnWriteArrrayList etc were added in Java 1.5.

BlockingQueue not only store elements but also supports flow control by introducing blocking if either BlockingQueue is full or empty. The take() method of BlockingQueue will block if Queue is empty and put() method of BlockingQueue will block if Queue is full.

Because of this property BlockingQueue is an ideal choice for implementing Producer-Consumer design pattern, where one thread insert element into BlockingQueue and other thread consumes it.

  • BlockingQueue in Java doesn't allow null elements, implementation of BlockingQueue like ArrayBlockingQueue, LinkedBlockingQueue throws NullPointerException when you try to add null on queue.
  • BlockingQueue can be bounded or unbounded. An unbounded Queue is one which is initialized without capacity, actually by default it initialized with Integer.MAX_VALUE. A bounded BlockingQueue is one which is initialized with initial capacity and call to put() will be blocked if BlockingQueue is full.
/*bounded BlockingQueue */
BlockingQueue < String > namebQueue = new ArrayBlockingQueue < String > (3);
namebQueue.put("Tiger Shroff");
System.out.println("Tiger Shroff is added in the list");
namebQueue.put("Salman Khan");
System.out.println("Salman Khan is added in the list");
namebQueue.put("Leonard D Cabrio");
System.out.println("Leonard D Cabrio is added in the list");
namebQueue.put("K Himaanshu Shuklaa"); //list is full, so this will not be added
System.out.println("K Himaanshu Shuklaa is added in the list");


Output will be:
Tiger Shroff is added in the list
Salman Khan is added in the list
Leonard D Cabrio is added in the list


  • BlockingQueue implementations like ArrayBlockingQueue, LinkedBlockingQueue and PriorityBlockingQueue are thread-safe. All queuing method uses concurrency control and internal locks to perform operation atomically.
  • BlockingQueue interface extends Collection, Queue and Iterable interface which provides it all Collection and Queue related methods like poll(), and peak(), unlike take(), peek() method returns head of the queue without removing it, poll() also retrieves and removes elements from head but can wait till specified time if Queue is empty.
  • Since BlockingQueue also extend Collection, bulk Collection operations like addAll(), containsAll() are not performed atomically until any BlockingQueue implementation specifically supports it. So call to addAll() may fail after inserting couple of elements.
Other important methods from BlockingQueue in Java are:
  • remainingCapacity() method of BlockingQueue, returns number remaining space in BlockingQueue, which can be filled without blocking.
  • offer() method of BlockingQueue insert object into queue if possible and return true if success and false if fail, unlike add() method which throws IllegalStateException if it fails to insert object into BlockingQueue. Use offer() over add() wherever possible.
-K Himaanshu Shuklaa..

No comments:

Post a Comment

RSSChomp Blog Directory