Skip to content

Commit 8db03ac

Browse files
committed
feat: add BoundedQueueDemo demonstrating producer-consumer with ArrayBlockingQueue
Implemented BoundedQueueDemo.java to illustrate bounded blocking queue behavior using ArrayBlockingQueue with fixed capacity. Demonstrates how put() and take() block when the queue is full or empty, showcasing thread synchronization between producer and consumer threads. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 5a95530 commit 8db03ac

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
A bounded queue** has a fixed maximum capacity — it cannot grow beyond its limit.
2+
3+
When the queue is full, producers attempting to insert elements block until space
4+
becomes available or until a timeout occurs (depending on the method used).
5+
6+
In Java:
7+
- `ArrayBlockingQueue` → always bounded**, capacity fixed at creation.
8+
- `LinkedBlockingQueue` → can be explicitly bounded via constructor.
9+
- `SynchronousQueue` → zero-capacity, special case where capacity = 0.
10+
11+
This design helps prevent memory exhaustion by throttling producers when consumers
12+
cannot keep up.
13+
14+
Bounded Queue (e.g., `ArrayBlockingQueue`)
15+
16+
┌────────────────────────────┐
17+
│ Bounded BlockingQueue │
18+
│────────────────────────────│
19+
Producer →│ [Slot1] [Slot2] [Slot3] │→ Consumer
20+
│────────────────────────────│
21+
│ • Fixed capacity (e.g., 3)│
22+
│ • put() blocks if full │
23+
│ • take() blocks if empty │
24+
│ • Ensures backpressure │
25+
└────────────────────────────┘
26+
27+
ASCII Flow Example
28+
29+
Producer threads:
30+
┌───────────┐ ┌───────────┐ ┌───────────┐
31+
│ Producer1 │ →→→ │ Producer2 │ →→→ │ Producer3 │
32+
└───────────┘ └───────────┘ └───────────┘
33+
│ │ │
34+
▼ ▼ ▼
35+
┌───────────────────────────────────────────────┐
36+
│ Queue: [Task1][Task2][Task3] (FULL) │
37+
└───────────────────────────────────────────────┘
38+
39+
40+
💤 Producers block until Consumer takes()
41+
42+
Consumer thread:
43+
┌───────────┐
44+
│ Consumer1 │ → consumes → frees space → producers resume
45+
└───────────┘
46+
47+
Summary of Behavior
48+
49+
| Queue Type | Capacity Type | put() when Full | take() when Empty | Example Use Case |
50+
|--------------------------|-------------------|----------------------------|----------------------------|----------------------------------|
51+
| `ArrayBlockingQueue` | Bounded | Blocks | Blocks | Thread throttling, load control |
52+
| `LinkedBlockingQueue` | Bounded/Unbounded | Blocks if bounded & full | Blocks | Mixed use, configurable |
53+
| `SynchronousQueue` | Zero-capacity | Always blocks until paired | Always blocks until paired | Direct thread handoff |
54+
55+
In short:
56+
A bounded queue enforces capacity limits and provides natural backpressure,
57+
ensuring that producers slow down when the queue is full. It’s ideal for systems
58+
where throughput must be controlled to prevent overloading memory or downstream
59+
consumers.
60+
61+
┌────────────────────────────┐
62+
│ Bounded BlockingQueue │
63+
│────────────────────────────│
64+
Producer →│ [Slot1] [Slot2] [Slot3] │→ Consumer
65+
│────────────────────────────│
66+
│ • Fixed capacity (e.g., 3)│
67+
│ • put() blocks if full │
68+
│ • take() blocks if empty │
69+
└────────────────────────────┘
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.concurrent.*;
2+
3+
public class BoundedQueueDemo {
4+
public static void main(String[] args) throws InterruptedException {
5+
BlockingQueue<String> queue = new ArrayBlockingQueue<>(2); // capacity = 2
6+
7+
// Producer Thread
8+
new Thread(() -> {
9+
try {
10+
for (int i = 1; i <= 5; i++) {
11+
queue.put("Task-" + i); // blocks when full
12+
System.out.println("Produced: Task-" + i);
13+
Thread.sleep(300);
14+
}
15+
} catch (InterruptedException e) {
16+
Thread.currentThread().interrupt();
17+
}
18+
}).start();
19+
20+
// Consumer Thread
21+
new Thread(() -> {
22+
try {
23+
while (true) {
24+
String task = queue.take(); // blocks when empty
25+
System.out.println("Consumed: " + task);
26+
Thread.sleep(1000);
27+
}
28+
} catch (InterruptedException e) {
29+
Thread.currentThread().interrupt();
30+
}
31+
}).start();
32+
}
33+
}

0 commit comments

Comments
 (0)