1.

What do you understand about BlockingQueue in Java?

Answer»

BlockingQueue is an INTERFACE that has been included along with a number of other concurrent Utility classes such as ConcurrentHashMap, Counting Semaphore, CopyOnWriteArrayList, and so on. In addition to queueing, the BlockingQueue interface enables flow control by adding blocking if either BlockingQueue is full or EMPTY

A THREAD attempting to enqueue an element in a full queue will be blocked until another thread clears the queue, either by dequeuing ONE or more elements or by clearing the queue entirely. It also prevents a thread from deleting from an empty queue until another thread inserts an item. A null value is not accepted by BlockingQueue. Implementations of the Java BlockingQueue interface are thread-safe. BlockingQueue's methods are all atomic and use internal locks or other forms of concurrency management.

There are two types of BlockingQueue in Java. They are as follows :

Unbounded Queue: The blocked queue's capacity will be set to Integer. MAX VALUE. An unbounded blocking queue will never block since it has the potential to grow to a very big SIZE. As you add more pieces, the size of the queue grows.

Example :

BlockingQueue unbounded_queue = new LinkedBlockingDeque();

Bounded Queue: The bounded queue is the second type of queue. In the case of a bounded queue, the capacity of the queue can be passed to the constructor when the blocking queue is created.

Example:

// Creates a Blocking Queue with capacity 10BlockingQueue bounded_queue = new LinkedBlockingDeque(10);


Discussion

No Comment Found