Clean • Professional
A BlockingQueue is a thread-safe queue in Java that blocks (waits) when:
It is part of java.util.concurrent package.
A BlockingQueue is a special queue designed for producer–consumer problems.
BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(5);
// Producer
queue.put(10); // waits if full
// Consumer
int value = queue.take(); // waits if empty

| Feature | Description |
|---|---|
| Thread-safe | All operations are synchronized internally |
| Blocking operations | put() waits if full, take() waits if empty |
| No null values | BlockingQueue does not allow null |
| Ideal for producer–consumer | Used in multi-threading applications |
| Method | Behavior |
|---|---|
put(e) | Inserts element, waits if queue is full |
take() | Removes element, waits if empty |
offer(e) | Inserts without waiting, returns false if full |
offer(e, time, unit) | Waits for limited time |
poll() | Removes and returns element, returns null if empty |
poll(time, unit) | Waits for limited time |
remainingCapacity() | Space left in queue |
BlockingQueue implementations come in two types :

put() blocks when fullExample (Bounded)
BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(5);
offer() never fails due to capacityput() never blocks because capacity never fillsExample (Unbounded)
BlockingQueue<Integer> queue = new LinkedBlockingQueue<>();

| Class | Type | Description |
|---|---|---|
| ArrayBlockingQueue | Bounded | Uses array (fixed size) |
| LinkedBlockingQueue | Bounded/Unbounded | Uses linked nodes |
| PriorityBlockingQueue | Unbounded | Priority-based |
| DelayQueue | Unbounded | Elements become available after delay |
| SynchronousQueue | No capacity | One element handoff (direct exchange) |
| LinkedTransferQueue | Unbounded | Fast transfer operations |
import java.util.concurrent.*;
public class BlockingQueueExample {
public static void main(String[] args) {
BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(5);
// Producer
Runnable producer = () -> {
try {
for (int i = 1; i <= 10; i++) {
System.out.println("Producing: " + i);
queue.put(i); // waits if full
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
};
// Consumer
Runnable consumer = () -> {
try {
while (true) {
int item = queue.take(); // waits if empty
System.out.println("Consuming: " + item);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
};
new Thread(producer).start();
new Thread(consumer).start();
}
}
Use BlockingQueue when your application needs:
| Feature | Bounded Queue | Unbounded Queue |
|---|---|---|
| Capacity | Fixed | No limit |
| put() | Blocks when full | Never blocks |
| Memory | Controlled | Can grow → high memory usage |
| Use-case | Backpressure control | Unlimited tasks |
| Examples | ArrayBlockingQueue | LinkedBlockingQueue(), PriorityBlockingQueue |