Java Reference
In-Depth Information
statement may lead to an incorrect withdraw. Using the loop statement, the withdraw task
will have a chance to recheck the condition before performing a withdraw.
Caution
Once a thread invokes await() on a condition, the thread waits for a signal to resume.
If you forget to call signal() or signalAll() on the condition, the thread will wait
forever.
ever-waiting threads
Caution
A condition is created from a Lock object. To invoke its method (e.g., await() ,
signal() , and signalAll() ), you must first own the lock. If you invoke these
methods without acquiring the lock, an IllegalMonitorStateException will be
thrown.
IllegalMonitorState-
Exception
Locks and conditions were introduced in Java 5. Prior to Java 5, thread communications were
programmed using the object's built-in monitors. Locks and conditions are more powerful
and flexible than the built-in monitor, so you will not need to use monitors. However, if you
are working with legacy Java code, you may encounter Java's built-in monitor.
A monitor is an object with mutual exclusion and synchronization capabilities. Only one
thread can execute a method at a time in the monitor. A thread enters the monitor by acquiring
a lock on it and exits by releasing the lock. Any object can be a monitor . An object becomes a
monitor once a thread locks it. Locking is implemented using the synchronized keyword on
a method or a block. A thread must acquire a lock before executing a synchronized method or
block. A thread can wait in a monitor if the condition is not right for it to continue executing
in the monitor. You can invoke the wait() method on the monitor object to release the lock
so that some other thread can get in the monitor and perhaps change the monitor's state. When
the condition is right, the other thread can invoke the notify() or notifyAll() method
to signal one or all waiting threads to regain the lock and resume execution. The template for
invoking these methods is shown in FigureĀ 30.17.
Java's built-in monitor
monitor
Task 1
Task 2
synchronized
(anObject) {
synchronized
(anObject) {
// When condition becomes true
anObject.notify(); or anObject.notifyAll();
...
try
{
// Wait for the condition to become true
while
(!condition)
anObject.wait();
// Do something when condition is true
catch (InterruptedException ex) {
ex.printStackTrace();
resume
}
}
}
F IGURE 30.17
The wait() , notify() , and notifyAll() methods coordinate thread communication.
The wait() , notify() , and notifyAll() methods must be called in a synchronized
method or a synchronized block on the receiving object of these methods. Otherwise, an
IllegalMonitorStateException will occur.
When wait() is invoked, it pauses the thread and simultaneously releases the lock on the
object. When the thread is restarted after being notified, the lock is automatically reacquired.
The wait() , notify() , and notifyAll() methods on an object are analogous to the
await() , signal() , and signalAll() methods on a condition.
 
 
Search WWH ::




Custom Search