48 lines
1.2 KiB
Plaintext
48 lines
1.2 KiB
Plaintext
== Why is this an issue?
|
|
|
|
The `java.util.concurrent.locks.Condition` interface provides an alternative to the `Object` monitor methods (`wait`, `notify` and `notifyAll`).
|
|
Hence, the purpose of implementing said interface is to gain access to its more nuanced `await` methods.
|
|
|
|
Consequently, calling the method `Object.wait` on a class implementing the `Condition` interface is contradictory and should be avoided. Use `Condition.await` instead.
|
|
|
|
== Code examples
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,java,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
void doSomething(Condition condition) {
|
|
condition.wait(); // Noncompliant, Object.wait is called
|
|
|
|
...
|
|
}
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,java,diff-id=1,diff-type=compliant]
|
|
----
|
|
void doSomething(Condition condition) {
|
|
condition.await(); // Compliant, Condition.await is called
|
|
|
|
...
|
|
}
|
|
----
|
|
|
|
== References
|
|
* https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Condition.html[Interface Condition - Java™ Platform SE 8, API Specification]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
The "Condition.await(...)" method should be used instead of "Object.wait(...)"
|
|
|
|
|
|
endif::env-github,rspecator-view[]
|