Single-Thread Wakeup
ID |
java.prefer_notifyall |
Severity |
high |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Java |
Tags |
concurrency, reliability |
Rationale
notify() wakes an arbitrary single thread out of however many are waiting on the object’s monitor. If more than one thread can be waiting at the same time, there is no guarantee the woken thread is the one whose condition just became true, and the other waiters stay asleep indefinitely. notifyAll() wakes every waiter so each can re-check its own condition.
// Bad - only one waiter wakes up, chosen arbitrarily
synchronized (lock) {
lock.notify();
}