This preview shows page 1. Sign up to view the full content.
Unformatted text preview: st evaluate to an object reference. If the
object is already locked by another thread, the thread is blocked
until the lock is released.
• When a lock is obtained on the object, the statements in the
synchronized block are executed, and then the lock is released.
37 Java’s Built-in Monitors
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 the monitor
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
38 wait(), notify(), and notifyAll()
Use the wait(), notify(), and notifyAll() methods to facilitate
communication among threads.
The wait(), notify...
View Full Document