<< Prev | Next >>
Here's a good link running through the exact mechanics of a Java monitor.
- Java multi-threading is implemented using synchronization. This coordinates multi-threaded access to common objects and methods.
- Java implementes synchronization using a monitor.
- Monitors can work by mutual exclusion, by using an object as a lock to protect code.
- Monitors can work by cooperation, by allowing threads to work together using wait and notify.
- Generally used when data is being shared. You would have a lock protecting shared data.
- A thread reaches a block of code protected by a monitor. This is called a monitor region.
- If the monitor is free, then the thread enters the monitor and can immediately acquire it.
- If the monitor is owned by a different thread, then the thread enters the monitor, but cannot acquire it. The thread waits in the entry set of the monitor.
- When the monitor becomes available then the thread can acquire the monitor and execute the code in the monitor region. There may be multiple threads waiting in the entry set. If this is the case then some kind of scheduling must be performed (FIFO or by priority I assume).
- When the thread exits the monitor region the monitor is released and exited.
- Helps threads work together towards some kind of common goal.
- Allows one to wait for another to complete a job and vice versa.
- Example: A reader waits for some data to arrive. A writer writes some data and notifies the reader that there is something for it to do.
- This is a wait and notify monitor, and is an extension of the mutually exclusive model described above.
- A thread that currently owns the monitor, can suspend itself inside the monitor by issueing a wait command. When this occurs the thread releases the monitor and enters the wait set. Note that the thread does not exit the monitor.
- A different thread will then acquire the monitor and may issue a notify command. When a thread does this, the usual pattern is for that thread to then release the monitor immediately.
- If a suspended thread is notified, reaquires the monitor and resumes execution, then it is not known who notified the thread. It is common for the thread to check that it has been notified for the correct reason and the state is as expected. It can always wait again.
- A monitor can be released by one of two things happening:
- A thread exiting a monitor region.
- A thread calling wait on the monitor.
- Notify vs NotifyAll. Notify marks only one of the threads in the wait set for execution. NotifyAll marks them all.
- Entering the monitor - Thread has reached a monitor region.
- Acquiring the monitor - Thread is scheduled to execute within the monitor region.
- Owning the monitor - Thread is executing within the monitor region.
- Releasing the monitor - A thread has released the monitor by either exiting the monitor region or by issueing a wait command.
- Exiting the monitor - A thread has exited the monitor region after releasing the monitor.
- Thread scheduling from the entry set and the wait set is implementation dependent. It is likely to be FIFO or by priority, or by a combination of the two.
<< Prev | Next >>