|
| 1 | +An unbounded queue is a queue that can grow indefinitely — |
| 2 | +it does not have a fixed capacity limit unless explicitly specified. |
| 3 | + |
| 4 | +In Java: |
| 5 | +- `LinkedBlockingQueue` → optionally bounded (default = unbounded, effectively `Integer.MAX_VALUE` capacity). |
| 6 | +- `DelayQueue`, `PriorityBlockingQueue` → always unbounded**. |
| 7 | + |
| 8 | +This means producers will never block on `put()` — new elements are always accepted, limited only by available memory. |
| 9 | + |
| 10 | +Unbounded Queue (e.g., `DelayQueue` or `LinkedBlockingQueue`) |
| 11 | + |
| 12 | + ┌────────────────────────────────────────────┐ |
| 13 | + │ Unbounded BlockingQueue │ |
| 14 | + │────────────────────────────────────────────│ |
| 15 | +Producer →│ [Task1] → [Task2] → [Task3] → [Task4] → … │→ Consumer |
| 16 | + │────────────────────────────────────────────│ |
| 17 | + │ • No fixed capacity limit │ |
| 18 | + │ • Grows dynamically (until memory limit) │ |
| 19 | + │ • put() never blocks │ |
| 20 | + │ • take() blocks if queue is empty │ |
| 21 | + └────────────────────────────────────────────┘ |
| 22 | + |
| 23 | +Bounded Queue (for contrast, e.g., `ArrayBlockingQueue`) |
| 24 | + |
| 25 | + ┌────────────────────────────┐ |
| 26 | + │ Bounded BlockingQueue │ |
| 27 | + │────────────────────────────│ |
| 28 | +Producer →│ [Slot1] [Slot2] [Slot3] │→ Consumer |
| 29 | + │────────────────────────────│ |
| 30 | + │ • Fixed capacity (e.g., 3)│ |
| 31 | + │ • put() blocks if full │ |
| 32 | + │ • take() blocks if empty │ |
| 33 | + └────────────────────────────┘ |
| 34 | + |
| 35 | +Summary of Behavior |
| 36 | + |
| 37 | +Queue Comparison Summary |
| 38 | + |
| 39 | +| Queue Type | Capacity Type | put() when Full | take() when Empty | Example Use Case | |
| 40 | +|----------------------|---------------|-----------------------------|----------------------------------|----------------------------------| |
| 41 | +| ArrayBlockingQueue | Bounded | Blocks | Blocks | Producer-consumer with throttling| |
| 42 | +| LinkedBlockingQueue | Unbounded | Never blocks | Blocks | General producer-consumer pattern| |
| 43 | +| DelayQueue | Unbounded | Never blocks | Blocks (until delay expires) | Task scheduling | |
| 44 | +| SynchronousQueue | Zero-capacity | Always blocks until paired | Always blocks until paired | Direct handoff | |
| 45 | + |
| 46 | + |
| 47 | +In short: |
| 48 | +An unbounded queue trades *backpressure control (throttling producers) for simplicity and flexibility — |
| 49 | +excellent for scenarios where the number of queued elements is small or predictable, but risky if production |
| 50 | +outpaces consumption. |
0 commit comments