BlockingQueue in Java
A BlockingQueue in Java is a type of queue that supports operations that wait for the queue to become non-empty when retrieving an element or wait for space to become available in the queue when storing an element. It is designed to be used in concurrent programming scenarios where multiple threads are producing and consuming elements from a shared queue.

Table of Contents
Here are some key points about Blocking Queue
1. Thread-Safe
Blocking Queue implementations are designed to be thread-safe, meaning they can be safely accessed and modified by multiple threads without the need for external synchronization mechanisms.
2. Blocking Operations
Its provides blocking operations such as put() and take(), which block the calling thread until space is available in the queue (for put()) or until an element becomes available (for take()).
3. Synchronous and Asynchronous Operations
Blocking Queue implementations can be both synchronous (blocking) and asynchronous (non-blocking), depending on the methods used for adding and removing elements. For example, put() and take() are synchronous operations, while offer() and poll() are asynchronous operations that return a boolean or null if the operation cannot be immediately fulfilled.
4. Support for Bounded and Unbounded Queues
The implementations can be either bounded or unbounded. Bounded queues have a fixed capacity, and attempts to add elements beyond the capacity will block until space becomes available. Unbounded queues have no fixed capacity and can grow dynamically.
5. Usage
Blocking Queue is commonly used in concurrent programming scenarios where multiple threads need to communicate and synchronize data exchange efficiently. It is often used in producer-consumer patterns, thread pools, and other scenarios where coordination between threads is required.
Now, let’s provide example using Blocking Queue
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class BlockingQueueExample {
public static void main(String[] args) {
// Create a Blocking-Queue with a capacity of 3
BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(3);
// Producer thread
Thread producer = new Thread(() -> {
try {
// Add elements to the queue
for (int i = 1; i <= 5; i++) {
System.out.println("Producing element: " + i);
queue.put(i);
Thread.sleep(1000); // Simulate some work
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
// Consumer thread
Thread consumer = new Thread(() -> {
try {
// Consume elements from the queue
while (true) {
int element = queue.take();
System.out.println("Consumed element: " + element);
Thread.sleep(2000); // Simulate some work
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
// Start producer and consumer threads
producer.start();
consumer.start();
}
}
BlockingQueue Example
In this example, we create a Blocking Queue using the ArrayBlocking Queue implementation, which has a fixed capacity of 3. We then create two threads: a producer thread that adds elements to the queue at regular intervals, and a consumer thread that removes elements from the queue and processes them. The put() and take() methods are used for adding and removing elements, respectively, and they block if necessary until space is available in the queue or until an element becomes available for retrieval.