...
The java.util.concurrent.locks
utilities provide the Condition.signal()
and Condition.signalAll()
methods to awaken threads that are blocked on a Condition.await()
call. Condition
objects are required when using java.util.concurrent.locks.Lock
objects. A Lock
object allows the use of Object.wait()
, Object.notify()
, and Object.notifyAll()
methods, however this is prohibited by rule "LCK03-J. Do not synchronize on the intrinsic locks of high-level concurrency objects". However, code Code that synchronizes using a Lock
object uses one or more Condition
objects associated with the Lock
object rather than using its own intrinsic lock. These objects interact directly with the locking policy enforced by the Lock
object. Consequently, the await()
, signal()
, and signalAll()
methods are used in place of the wait()
, notify()
, and notifyAll()
methods.
...
This noncompliant code example shows a complex multistep multi-step process being undertaken by several threads. Each thread executes the step identified by the time field. Each thread waits for the time
field to indicate that it is time to perform the corresponding thread's step. After performing the step, each thread increments time
and then notifies the thread that is responsible for the next step.
...
Code Block | ||
---|---|---|
| ||
public final class ProcessStep implements Runnable {
private static final Object lock = new Object();
private static int time = 0;
private final int step; // Do operations when field time reaches this value
public ProcessStep(int step) {
this.step = step;
}
@Override public void run() {
try {
synchronized (lock) {
while (time != step) {
lock.wait();
}
// Perform operations
time++;
lock.notifyAll(); // Use notifyAll() instead of notify()
}
} catch (InterruptedException ie) {
Thread.currentThread().interrupt(); // Reset interrupted status
}
}
}
|
...
Code Block | ||
---|---|---|
| ||
public class ProcessStep implements Runnable {
private static final Lock lock = new ReentrantLock();
private static final Condition condition = lock.newCondition();
private static int time = 0;
private final int step; // Do operations when field time reaches this value
public ProcessStep(int step) {
this.step = step;
}
@Override public void run() {
lock.lock();
try {
while (time != step) {
condition.await();
}
// Perform operations
time++;
condition.signalAll();
} catch (InterruptedException ie) {
Thread.currentThread().interrupt(); // Reset interrupted status
} finally {
lock.unlock();
}
}
}
|
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="fa88cb09ede3e43d-17c3188f-4d4b40ae-b2669709-fcd2aa170c1f3a17ae8c1bd8"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] | | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="b340285af6211c9f-6a335acc-45644ff9-979aae64-05362862dc99fc76b34b69ea"><ac:plain-text-body><![CDATA[ | [[JLS 2005 | AA. Bibliography#JLS 05]] | [Chapter 17, Threads and Locks | http://java.sun.com/docs/books/jls/third_edition/html/memory.html] | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="66a88b2c42e3c18f-4c42a66e-4e1648c0-a57e9cc4-4c338427ede5e878f3bc451c"><ac:plain-text-body><![CDATA[ | [[Goetz 2006 | AA. Bibliography#Goetz 06]] | Section 14.2.4, Notification | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="0dbbc7084a25adc6-d66cc368-4f7e4338-9f9ebd89-b24f5e5066269a364e03a126"><ac:plain-text-body><![CDATA[ | [[Bloch 2001 | AA. Bibliography#Bloch 01]] | Item 50: Never invoke wait outside a loop | ]]></ac:plain-text-body></ac:structured-macro> |
...