0% found this document useful (0 votes)
4 views12 pages

Understanding Java's BlockingQueue Interface

Uploaded by

nvpsslakshman
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views12 pages

Understanding Java's BlockingQueue Interface

Uploaded by

nvpsslakshman
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

BlockingQueue

Interface in Java
Introduction
• The BlockingQueue interface in Java is added in Java 1.5 along
with various other concurrent Utility classes like
ConcurrentHashMap, Counting Semaphore,
CopyOnWriteArrrayList, etc.

• BlockingQueue interface supports flow control (in addition to


queue) by introducing blocking if either BlockingQueue is full or
empty.

• A thread trying to enqueue an element in a full queue is blocked


until some other thread makes space in the queue, either by
dequeuing one or more elements or clearing the queue completely.
• Similarly, it blocks a thread trying to delete from an
empty queue until some other threads insert an item.
BlockingQueue does not accept a null value. If we try to
enqueue the null item, then it
throws NullPointerException.

• Java provides several BlockingQueue implementations


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

• Java 5 comes with BlockingQueue implementations in


the [Link] package.
Usage of BlockingQueue
The Hierarchy of BlockingQueue
Declaration

public interface BlockingQueue<E> extends


Queue<E>
Here, E is the type of elements stored in the Collection.
Classes that Implement
BlockingQueue
• We directly cannot provide an instance of
BlockingQueue since it is an interface, so to utilize the
functionality of the BlockingQueue, we need to make
use of the classes implementing it.
• Also, to use BlockingQueue in your code, use this import
statement.

• import [Link];
(or)
import [Link].*;
• The implementing class of BlockingDeque is
LinkedBlockingDeque. This class is the implementation
of the BlockingDeque and the linked list data structure.

• The LinkedBlockingDeque can be optionally bounded


using a constructor, however, if the capacity is
unspecified it is Integer.
• MAX_VALUE by default.
• The nodes are added dynamically at the time of
insertion obeying the capacity constraints.
The syntax for creating objects:

BlockingQueue<?> objectName = new LinkedBlockingDeque<?>();


(or)
LinkedBlockingDeque<?> objectName = new
LinkedBlockingDeque<?>();
BlockingQueue Types

• The BlockingQueue are two types:


• 1. Unbounded Queue: The Capacity of the blocking
queue will be set to Integer.MAX_VALUE. In the case of
an unbounded blocking queue, the queue will never
block because it could grow to a very large size. when
you add elements its size grows.
• Syntax:
• BlockingQueue blockingqueue = new Linked BlockingDeque();
• 2. Bounded Queue: The second type of queue is the
bounded queue. In the case of a bounded queue you
can create a queue passing the capacity of the queue in
queues constructor:

• Syntax:
• //create a blocking queue with capacity 5
• BlockingQueue blockingqueue = new
LinkedBlockingDeque(5);

You might also like