Bounded Blocking Queue (M Producers, N Consumers using non-thread-safe queue)
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2023-12-11
Problem Statement
Designing a bounded blocking queue that allows M producers and N consumers to safely interact with a non-thread-safe underlying queue.
The queue must block:
•Producers when the queue is full
•Consumers when the queue is empty
Focus is on thread safety, synchronization, and race condition avoidance.
✅ Functional Requirements
1.Queue Operations
•enqueue(item): Add an item to the queue. Block if full.
•dequeue(): Remove an item from the queue. Block if empty.
2.Concurrency
•Support multiple producer threads and multiple consumer threads concurrently.
•Ensure thread-safe access to the underlying non-thread-safe queue.
3.Capacity Management
•Fixed maximum capacity.
•Queue blocks producers when capacity is reached.
4.Optional Features
•Timeout-based enqueue/dequeue
•Peek operation
•Metrics for queue utilization
🔐 Constraints & Assumptions
•Underlying queue (e.g., LinkedList in Java or deque in C++) is not thread-safe.
•Focus on LLD, concurrency primitives, and blocking behavior.
•Single-machine implementation.
•Items are generic objects (int, string, or custom type).
🧱 Key Components & Classes
1.BoundedBlockingQueue Class
1
2class BoundedBlockingQueue<T> {
3 private Queue<T> queue; // Non-thread-safe queue
4 private int capacity;
5 private final Object lock = new Object();
6
7 public BoundedBlockingQueue(int capacity) {
8 this.capacity = capacity;
9 this.queue = new LinkedList<>();
10 }
11
12 public void enqueue(T item) throws InterruptedException {
13 synchronized (lock) {
14 while (queue.size() == capacity) {
15 lock.wait(); // Wait if full
16 }
17 queue.add(item);
18 lock.notifyAll(); // Notify consumers
19 }
20 }
21
22 public T dequeue() throws InterruptedException {
23 synchronized (lock) {
24 while (queue.isEmpty()) {
25 lock.wait(); // Wait if empty
26 }
27 T item = queue.remove();
28 lock.notifyAll(); // Notify producers
29 return item;
30 }
31 }
32
33 public int size() {
34 synchronized (lock) {
35 return queue.size();
36 }
37 }
38}🔄 Example Workflow
1.Producer Thread (M threads):
•Calls enqueue(item)
•Blocks if queue is full
2.Consumer Thread (N threads):
•Calls dequeue()
•Blocks if queue is empty
3.Multiple Threads:
•Thread-safe operations ensured using a single lock + wait/notify
🧪 Expected Deliverables
•Class diagram showing BoundedBlockingQueue and producer/consumer interactions.
•Implementation of enqueue and dequeue methods with blocking behavior.
•Test cases:
•Multiple producers and consumers concurrently
•Verify blocking when full/empty
•Thread safety under high concurrency
•Correct ordering of elements (FIFO)
💡 Discussion Points / Trade-offs
1.Locking Strategy
•Single lock for simplicity → may limit throughput under high contention.
•Fine-grained locks or separate conditions for producer/consumer → more complex but higher concurrency.
2.Blocking vs Non-blocking
•Blocking ensures safety for producer-consumer workflow.
•Optional non-blocking methods can allow timeouts.
3.Queue Choice
•Non-thread-safe queue used internally → all access must be synchronized.
4.Extensibility
•Add metrics for queue size and utilization
•Support multiple priority levels for tasks
This problem tests LLD, concurrency, blocking behavior, and safe access to non-thread-safe structures, making it ideal for an LLD + multithreading interview round.