notify and notifyAll both wake up sleeping threads waiting on the object’s monitor, but notify only wakes up
one single thread, while notifyAll wakes them all up. Unless you do not care which specific thread is woken up, notifyAll
should be used instead.
Noncompliant code example
class MyThread implements Runnable {
Object lock = new Object();
@Override
public void run() {
synchronized(lock) {
// ...
lock.notify(); // Noncompliant
}
}
}
Compliant solution
class MyThread implements Runnable {
Object lock = new Object();
@Override
public void run() {
synchronized(lock) {
// ...
lock.notifyAll();
}
}
}