rspec/rules/S2446/java/rule.adoc

66 lines
1.2 KiB
Plaintext

== Why is this an issue?
`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
[source,java,diff-id=1,diff-type=noncompliant]
----
class MyThread implements Runnable {
Object lock = new Object();
@Override
public void run() {
synchronized(lock) {
// ...
lock.notify(); // Noncompliant
}
}
}
----
=== Compliant solution
[source,java,diff-id=1,diff-type=compliant]
----
class MyThread implements Runnable {
Object lock = new Object();
@Override
public void run() {
synchronized(lock) {
// ...
lock.notifyAll();
}
}
}
----
== Resources
* https://wiki.sei.cmu.edu/confluence/x/MTdGBQ[CERT, THI02-J.] - Notify all waiting threads rather than a single thread
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
"notify" may not wake up the appropriate thread.
'''
== Comments And Links
(visible only on this page)
=== relates to: S3046
endif::env-github,rspecator-view[]