How to use BlockingQueue to achieve Concurrency? [Part-1]

Shubhmeet Kaur
3 min readJun 20, 2020

This post is part 1 of How to use using java.util.concurrent to achieve concurrency in java and solve multi-threading real world problems.

Java provides BlockingQueue interface and has several implementations such as LinkedBlockingQueue, ArrayBlockingQueue, PriorityBlockingQueue, SynchronousQueue. These interface implementations are thread-safe. All methods of BlockingQueue are atomic in nature and use internal locks or other forms of concurrency control.

Types of Queue:

  1. Bounded Queue: The capacity of this queue is limited to the number defined by the value given in the constructor. Once, the queue reaches it’s capacity, any further add operations will be blocked and Illegal State Exception is thrown.
BlockingQueue blockingQueue = new LinkedBlockingDeque(5);
//capacity of this queue is 5

2. Unbounded Queue : The capacity of this queue is Integer.MAX_VALUE. All add operations are non-blocking and queue can grow up-to any capacity

BlockingQueue blockingQueue = new LinkedBlockingDeque();

LinkedBlockingQueue

LinkedBlockingQueue is an optionally bounded queue, which means capacity of the queue can be bounded or can left be unspecified. In LinkedBlockingQueue, each linked nodes stores the link to the next node. These nodes are created dynamically based on the insertion order. This queue follows First in First Out (FIFO). The first element is the oldest element called as head. The head element is in queue for the longest time and will be the first one to be removed on a remove call. The last element also called as the tail is the one which has been in the queue for the shortest time.

Features of LinkedBlockingQueue:

  1. LinkedBlocking Queues is an optionally bounded queue
  2. Java’s implementation for LinkedBlockingQueue is thread safe
  3. All methods are achieved atomically and uses Reentrant Lock internally
  4. Null values are not allowed in the queue. Trying to add Null values in LinkedBlocking queue will lead to Null Pointer Exception

Initializing LinkedBlockingQueue Constructor

  1. LinkedBlockingQueue() — creates LinkedBlockingQueue with Integer.MAX_VALUE as capacity
  2. LinkedBlockingQueue(int size) — creates LinkedBlockingQueue with the fixed given capacity
  3. LinkedBlockingQueue(Collection<? extends E> c) — creates a LinkedBlockingQueue with Integer.MAX_VALUE as capacity of queue. The elements of the given collection will be added to the queue with the traversal order of the collection’s iterator

Adding Elements to LinkedBlockingQueue

  1. add() — returns true if insertion was a success, otherwise throws an IllegalStateException
  2. offer() — returns true if insertion was a success, otherwise false
  3. put() — inserts the specified element into a queue, waits in case of the queue is full until a slot to be become available
  4. offer(E e, long timeout, TimeUnit unit) — tries to insert element into a queue and waits for specified timeout in case the queue is full for a slot to be become available

Removing Elements from LinkedBlockingQueue

  1. remove() — retrieves and removes the head of the queue, or throws NoSuchElementException if the queue is empty
  2. poll() —retrieves and removes the head of the queue, and null in case the queue is empty
  3. take() —retrieves and removes the head of the queue. If the queue is empty, the call is blocked and waits for an element to become available
  4. poll(long timeout, TimeUnit unit) — retrieves and removes the head of the queue. If the queue is empty, the call is blocked and waits for an element to become available for the specified time. Returns null after the timeout passes

Retrieval Elements from LinkedBlockingQueue

  1. element() — retrieves the head element of the queue or throws NoSuchElementException if the queue is empty
  2. peek() — retrieves the head element of the queue or null in case the queue is empty

These methods will be heavily used to illustrate the real world application of LinkedBlockingQueue to solve multi-threaded famous producer consumer problem. Stay tuned for next post: How to use BlockingQueue to achieve Concurrency in Java?Part-2. Feel free to reach out and give any feedback.

References:

Thank you!

Shubhmeet

--

--

Shubhmeet Kaur

Software Engineer | Graduated MSCS,Fall 2018 | Code Enthusiastic