2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-06-07 17:17:22 +02:00
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.
2021-04-28 16:49:39 +02:00
2023-06-07 17:17:22 +02:00
Consequently, calling the method `Object.wait` on a class implementing the `Condition` interface is contradictory and should be avoided. Use `Condition.await` instead.
2021-04-28 16:49:39 +02:00
2023-06-07 17:17:22 +02:00
== Code examples
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2023-06-07 17:17:22 +02:00
[source,java,diff-id=1,diff-type=noncompliant]
2021-04-28 16:49:39 +02:00
----
2023-06-07 17:17:22 +02:00
void doSomething(Condition condition) {
condition.wait(); // Noncompliant, Object.wait is called
...
}
2021-04-28 16:49:39 +02:00
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-04-28 16:49:39 +02:00
2023-06-07 17:17:22 +02:00
[source,java,diff-id=1,diff-type=compliant]
2021-04-28 16:49:39 +02:00
----
2023-06-07 17:17:22 +02:00
void doSomething(Condition condition) {
condition.await(); // Compliant, Condition.await is called
...
}
2021-04-28 16:49:39 +02:00
----
2021-04-28 18:08:03 +02:00
2023-06-07 17:17:22 +02:00
== References
* https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Condition.html[Interface Condition - Java™ Platform SE 8, API Specification]
2021-09-20 15:38:42 +02:00
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
The "Condition.await(...)" method should be used instead of "Object.wait(...)"
2021-09-20 15:38:42 +02:00
endif::env-github,rspecator-view[]