Blocking queues and producer-consumer
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
build a bounded pipeline with backpressure.
Planned topics
BlockingQueue:putblocks when full,takeblocks when empty;offer/pollaccept timeouts.ArrayBlockingQueueis bounded;LinkedBlockingQueueis unbounded unless given a capacity.- A bounded queue gives backpressure: fast producers slow down instead of exhausting memory.
- Stopping consumers: send a poison pill, or interrupt the workers.
- Choose queue size from latency tolerance, not "large to be safe".
Draft outline details
Demonstration
BlockingQueue<OrderEvent> q = new ArrayBlockingQueue<>(1_000);
// producer: q.put(event); consumer: var e = q.take();Check your understanding
What happens when a producer calls put on a full queue?
Show answer
It blocks until space is free.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.