rspec/rules/S1844/java/rule.adoc

30 lines
1.0 KiB
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
From the Java API documentation:
____
``++Condition++`` factors out the ``++Object++`` monitor methods (``++wait++``, ``++notify++`` and ``++notifyAll++``) into distinct objects to give the effect of having multiple wait-sets per object, by combining them with the use of arbitrary Lock implementations. Where a ``++Lock++`` replaces the use of ``++synchronized++`` methods and statements, a ``++Condition++`` replaces the use of the ``++Object++`` monitor methods.
____
The purpose of implementing the ``++Condition++`` interface is to gain access to its more nuanced ``++await++`` methods. Therefore, calling the method ``++Object.wait(...)++`` on a class implementing the ``++Condition++`` interface is silly and confusing.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
----
final Lock lock = new ReentrantLock();
final Condition notFull = lock.newCondition();
...
notFull.wait();
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
----
final Lock lock = new ReentrantLock();
final Condition notFull = lock.newCondition();
...
notFull.await();
----