Commit e49cd55
authored
feat(datastructures): add thread-safe bounded queue implementation (#7428)
* feat(datastructures): add thread-safe bounded queue implementation
Implements a thread-safe blocking queue using ReentrantLock and
Condition variables for producer-consumer synchronization.
### What This Adds
**ThreadSafeQueue.java** - Thread-safe bounded queue:
- `enqueue()` - Blocking add to tail, waits when queue is full
- `dequeue()` - Blocking remove from head, waits when queue is empty
- `offer()` - Non-blocking add, returns false when full
- `poll()` - Non-blocking remove, returns null when empty
- `size()`, `isEmpty()`, `isFull()`, `capacity()` - State queries
- Uses circular buffer for O(1) enqueue/dequeue operations
- Supports multiple concurrent producers and consumers
**ThreadSafeQueueTest.java** - Comprehensive test suite:
- Basic enqueue/dequeue operations
- Offer/poll non-blocking behavior
- Null rejection validation
- Invalid capacity rejection
- Circular buffer wrap-around
- Multiple producers single consumer concurrency
- Single producer multiple consumers concurrency
- Blocking behavior verification
- Stress test with 8 concurrent threads
### Algorithm
Uses a circular buffer with ReentrantLock and two Condition variables:
- `notFull` - signaled when space becomes available
- `notEmpty` - signaled when items are added
- Producers await notFull when buffer is full
- Consumers await notEmpty when buffer is empty
- Signal opposite condition after each operation
Time: O(1) enqueue/dequeue | Space: O(n) bounded buffer
### Reference
https://en.wikipedia.org/wiki/Producer%E2%80%93consumer_problem
* fix(datastructures): correct test capacity and simplify concurrent test
- testOfferPoll: Changed capacity from 3 to 2 so third offer correctly fails
- testMultipleProducersSingleConsumer: Removed startLatch, use dedicated
consumer thread with synchronized results list for thread safety
* fix(datastructures): remove unused assertArrayEquals import
Checkstyle flagged UnusedImports violation for
org.junit.jupiter.api.Assertions.assertArrayEquals which was
not used in any test method.
* fix: replace signal() with signalAll() to satisfy SpotBugs MDM_SIGNAL_NOT_SIGNALALL
SpotBugs flags all four Condition.signal() calls in ThreadSafeQueue as Medium
severity bugs (MDM_SIGNAL_NOT_SIGNALALL). In a multi-producer/multi-consumer
scenario, signal() wakes only one waiting thread, which can cause deadlock when
multiple producers or consumers are blocked on the same condition variable.
Using signalAll() ensures all waiting threads are notified and can re-check
their loop condition, preventing the lost-wakeup problem that occurs when a
single signal wakes a thread that cannot make progress.
This change affects enqueue(), dequeue(), offer(), and poll() methods where
notEmpty.signal() and notFull.signal() are replaced with notEmpty.signalAll()
and notFull.signalAll() respectively.
* test: replace static imports with Assertions prefix to satisfy PMD TooManyStaticImports
PMD flags TooManyStaticImports when more than 4 static imports are present.
The test file had 5 static imports from org.junit.jupiter.api.Assertions
(equals, assertFalse, assertNull, assertThrows, assertTrue) which exceeded
the default threshold. Replaced with regular import and Assertions.
prefix to eliminate the PMD violation while maintaining readability.1 parent 0a62b11 commit e49cd55
2 files changed
Lines changed: 481 additions & 0 deletions
File tree
- src
- main/java/com/thealgorithms/datastructures/queues
- test/java/com/thealgorithms/datastructures/queues
Lines changed: 186 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
0 commit comments