Skip to content

Commit 4c7db65

Browse files
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.
1 parent 301b1e8 commit 4c7db65

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

src/main/java/com/thealgorithms/datastructures/queues/ThreadSafeQueue.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void enqueue(T item) throws InterruptedException {
6060
buffer[tail] = item;
6161
tail = (tail + 1) % capacity;
6262
count++;
63-
notEmpty.signal();
63+
notEmpty.signalAll();
6464
} finally {
6565
lock.unlock();
6666
}
@@ -82,7 +82,7 @@ public T dequeue() throws InterruptedException {
8282
buffer[head] = null;
8383
head = (head + 1) % capacity;
8484
count--;
85-
notFull.signal();
85+
notFull.signalAll();
8686
return item;
8787
} finally {
8888
lock.unlock();
@@ -108,7 +108,7 @@ public boolean offer(T item) {
108108
buffer[tail] = item;
109109
tail = (tail + 1) % capacity;
110110
count++;
111-
notEmpty.signal();
111+
notEmpty.signalAll();
112112
return true;
113113
} finally {
114114
lock.unlock();
@@ -130,7 +130,7 @@ public T poll() {
130130
buffer[head] = null;
131131
head = (head + 1) % capacity;
132132
count--;
133-
notFull.signal();
133+
notFull.signalAll();
134134
return item;
135135
} finally {
136136
lock.unlock();

0 commit comments

Comments
 (0)