<< Prev | Next >>

Java Monitors

Here's a good link running through the exact mechanics of a Java monitor.

Notes

  • Java multi-threading is implemented using synchronization. This coordinates multi-threaded access to common objects and methods.
  • Java implementes synchronization using a monitor.
  1. Monitors can work by mutual exclusion, by using an object as a lock to protect code.
  2. Monitors can work by cooperation, by allowing threads to work together using wait and notify.
Mutual Exclusion
  • Generally used when data is being shared. You would have a lock protecting shared data.
  1. A thread reaches a block of code protected by a monitor. This is called a monitor region.
  2. If the monitor is free, then the thread enters the monitor and can immediately acquire it.
  3. 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.
  4. 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).
  5. When the thread exits the monitor region the monitor is released and exited.
Cooperation
  • 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.
  1. 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.
  2. 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.
  3. 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.
  4. A monitor can be released by one of two things happening:
    1. A thread exiting a monitor region.
    2. A thread calling wait on the monitor.
  5. Notify vs NotifyAll. Notify marks only one of the threads in the wait set for execution. NotifyAll marks them all.
Summary
  1. Entering the monitor - Thread has reached a monitor region.
  2. Acquiring the monitor - Thread is scheduled to execute within the monitor region.
  3. Owning the monitor - Thread is executing within the monitor region.
  4. Releasing the monitor - A thread has released the monitor by either exiting the monitor region or by issueing a wait command.
  5. Exiting the monitor - A thread has exited the monitor region after releasing the monitor.
  6. 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.

Posted by Scott Dupoy - 2005-07-20 10:28:26

<< Prev | Next >>