|
| 1 | +An unbounded queue has no fixed capacity limit — it can grow dynamically |
| 2 | +until available system memory is exhausted. |
| 3 | + |
| 4 | +Producers adding elements never block, as the queue always accepts new items. |
| 5 | + |
| 6 | +In Java: |
| 7 | +- `LinkedBlockingQueue` → optionally bounded, but unbounded by default (capacity = Integer.MAX_VALUE). |
| 8 | +- `PriorityBlockingQueue` → always unbounded, ordered by priority or natural order. |
| 9 | +- `DelayQueue` → always unbounded, elements become available only after their delay expires. |
| 10 | + |
| 11 | +This design is useful when the number of elements is unpredictable and memory |
| 12 | +resources are sufficient. |
| 13 | + |
| 14 | +Unbounded Queue (e.g., `LinkedBlockingQueue`, `DelayQueue`) |
| 15 | + |
| 16 | + ┌────────────────────────────────────────────┐ |
| 17 | + │ Unbounded BlockingQueue │ |
| 18 | + │────────────────────────────────────────────│ |
| 19 | +Producer →│ [Task1] → [Task2] → [Task3] → [Task4] → … │→ Consumer |
| 20 | + │────────────────────────────────────────────│ |
| 21 | + │ • No fixed capacity limit │ |
| 22 | + │ • Grows dynamically (until memory limit) │ |
| 23 | + │ • put() never blocks │ |
| 24 | + │ • take() blocks if queue is empty │ |
| 25 | + └────────────────────────────────────────────┘ |
| 26 | + |
| 27 | + |
| 28 | +Producer threads: |
| 29 | +┌───────────┐ ┌───────────┐ ┌───────────┐ |
| 30 | +│ Producer1 │ →→→ │ Producer2 │ →→→ │ Producer3 │ |
| 31 | +└───────────┘ └───────────┘ └───────────┘ |
| 32 | + │ │ │ |
| 33 | + ▼ ▼ ▼ |
| 34 | +┌──────────────────────────────────────────────────┐ |
| 35 | +│ Queue: [Task1][Task2][Task3][Task4][Task5]…∞ │ |
| 36 | +└──────────────────────────────────────────────────┘ |
| 37 | + │ |
| 38 | + ▼ |
| 39 | + 🚀 Keeps accepting new tasks (until memory full) |
| 40 | + |
| 41 | +Consumer thread: |
| 42 | +┌───────────┐ |
| 43 | +│ Consumer1 │ → takes → processes → repeats |
| 44 | +└───────────┘ |
| 45 | + |
| 46 | + |
| 47 | +Summary of Behavior |
| 48 | + |
| 49 | +| Queue Type | Capacity Type | put() when Full | take() when Empty| Example Use Case | |
| 50 | +|--------------------------|---------------|------------------|------------------|-------------------------------------| |
| 51 | +| `LinkedBlockingQueue` | Unbounded | Never blocks | Blocks | Generic producer-consumer model | |
| 52 | +| `PriorityBlockingQueue` | Unbounded | Never blocks | Blocks | Priority-based task scheduling | |
| 53 | +| `DelayQueue` | Unbounded | Never blocks | Blocks (until delay expires) | Delayed job execution | |
| 54 | + |
| 55 | +In short: |
| 56 | +An unbounded queue favors flexibility and throughput by eliminating |
| 57 | +capacity restrictions. |
| 58 | + |
| 59 | +It is ideal for applications where queue size is expected to remain reasonable, |
| 60 | +but it can be risky if producers outpace consumers — potentially causing |
| 61 | +OutOfMemoryError due to uncontrolled growth. |
| 62 | + |
| 63 | + |
| 64 | + ┌────────────────────────────────────────────┐ |
| 65 | + │ Unbounded BlockingQueue │ |
| 66 | + │────────────────────────────────────────────│ |
| 67 | +Producer →│ [Task1] → [Task2] → [Task3] → [Task4] → … │→ Consumer |
| 68 | + │────────────────────────────────────────────│ |
| 69 | + │ • No fixed capacity limit │ |
| 70 | + │ • Grows dynamically (until memory limit) │ |
| 71 | + │ • put() never blocks │ |
| 72 | + │ • take() blocks if queue is empty │ |
| 73 | + └────────────────────────────────────────────┘ |
0 commit comments