BlockingQueue
A BlockingQueue is a thread-safe queue whose put waits when the queue is full and whose take waits when the queue is empty. It is the standard hand-off between producer and consumer threads.
Detailed Explanation
Two threads need to pass work. A plain ArrayDeque is not safe. A concurrent queue that never blocks (ConcurrentLinkedQueue) forces the consumer to spin or sleep. A BlockingQueue parks the producer when capacity is gone and parks the consumer when there is nothing to take. That back-pressure is the point.
Methods again come in families. put/take block indefinitely. offer/poll with a timeout wait a while and then return false/null. offer/poll without a timeout do not wait. add/remove throw. Use put/take for a pipeline you want to run until shutdown, and timed offer/poll when you must stay responsive.
Implementations: ArrayBlockingQueue is bounded and array-backed, fair or not. LinkedBlockingQueue is optionally bounded (default Integer.MAX_VALUE, which is "almost unbounded" and can exhaust memory). SynchronousQueue has no storage; put waits for a take. PriorityBlockingQueue is an unbounded priority heap. DelayQueue releases elements only after a delay.
null is forbidden. Shutdown is a design problem: poison-pill objects, ExecutorService interruption, or a separate running flag. Interrupting a thread blocked in take throws InterruptedException; restore the interrupt flag if you do not handle it.
You reach for a blocking queue whenever one thread produces and another consumes, including thread-pool work queues (ThreadPoolExecutor uses one). You size the bound from memory and latency: too small and producers stall, too large and you hide slowness until the heap fills.
The cost of an unbounded linked blocking queue is a silent memory leak under a slow consumer. The cost of SynchronousQueue is coupling: a producer cannot get ahead at all. The cost of forgetting to interrupt on shutdown is a thread parked forever.
Code Examples
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class Handoff {
public static void main(String[] args) throws InterruptedException {
BlockingQueue<String> q = new ArrayBlockingQueue<>(2);
q.put("a");
q.put("b");
System.out.println(q.take()); // prints: a
System.out.println(q.offer("c")); // prints: true
System.out.println(q.size()); // prints: 2
}
}import java.util.concurrent.SynchronousQueue;
public class Rendezvous {
public static void main(String[] args) throws InterruptedException {
SynchronousQueue<Integer> q = new SynchronousQueue<>();
Thread producer = new Thread(() -> {
try {
q.put(7); // waits until take()
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
producer.start();
System.out.println(q.take()); // prints: 7
producer.join();
}
}Common Gotchas
`new LinkedBlockingQueue()` is nearly unbounded
a slow consumer can fill the heap. Pass a capacity.
`null` is rejected
use a poison-pill instance of your payload type for shutdown, not null.
Always handle `InterruptedException`
restore the interrupt status if you abort.
`offer` without a timeout does not block
a full `ArrayBlockingQueue` then returns `false`.