Part 2 of 6 · 2 min read
The pattern
Separate finding work from doing work.
Think of two people washing dishes. One washes and puts each dish in a rack. The other takes dishes from the rack and dries them. The rack lets them work at different speeds. If the rack is empty, the dryer waits. If it is full, the washer waits. JCIP uses this same picture.
JCIP describes the producer-consumer design as one that separates identifying work from doing it. Work items go onto a to-do list, and are processed later and not at once. Producers need not know how many consumers there are, or who they are. Consumers need not know where work came from. The two sides share only the queue. The queue is a blocking queue when put waits for room and take waits for an item.
An email sender shows it. Booking code produces mail jobs. One consumer thread sends them.
BlockingQueue<String> q = new ArrayBlockingQueue<>(2);
Thread consumer = new Thread(() -> {
try {
while (true) {
String job = q.take(); // waits if empty
if (job == STOP) break;
send(job);
}
} catch (InterruptedException e) { Thread.currentThread().interrupt(); }
});
consumer.start();
for (int i = 1; i <= 6; i++) q.put("mail" + i); // waits if full
q.put(STOP);
consumer.join();
Six jobs go through a queue that holds only two. When the queue is full, put blocks, so the producer is slowed to the speed of the consumer. All six are sent, in order. The queue is the only shared object, and its methods are already thread-safe. No explicit lock appears in the code.
The size limit matters. JCIP warns that if producers are consistently faster than consumers, an unbounded queue grows until the program runs out of memory. A bounded queue pushes back on the producers. This is called back pressure, and it keeps memory use steady.
Producer-consumer
The producer-consumer pattern puts work on a shared queue from one side and takes it off from the other, so the two sides run at their own speeds.
Example: booking code adds mail jobs, and a sender thread takes them.
Check yourself:
1. What happens when the producer calls put on a full queue?
2. Why is a bounded queue safer than an unbounded one?
3. What do producer and consumer share?