40 lines
735 B
Plaintext
40 lines
735 B
Plaintext
``++notify++`` and ``++notifyAll++`` both wake up sleeping threads, but ``++notify++`` only rouses one, while ``++notifyAll++`` rouses all of them. Since ``++notify++`` might not wake up the right thread, ``++notifyAll++`` should be used instead.
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
----
|
|
class MyThread extends Thread{
|
|
|
|
@Override
|
|
public void run(){
|
|
synchronized(this){
|
|
// ...
|
|
notify(); // Noncompliant
|
|
}
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
----
|
|
class MyThread extends Thread{
|
|
|
|
@Override
|
|
public void run(){
|
|
synchronized(this){
|
|
// ...
|
|
notifyAll();
|
|
}
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
== See
|
|
|
|
* https://wiki.sei.cmu.edu/confluence/x/MTdGBQ[CERT, THI02-J.] - Notify all waiting threads rather than a single thread
|
|
|