Monday, December 23, 2013

Monitors – The Basic Idea of Java synchronization

If you took operating system course, you would know monitor is an important concept of synchronization in operating systems. It is also used in Java synchronization.
1. What is a Monitor?
A monitor is like a building that contains a special room which can be occupied by only one customer(thread) at a time. The room usually contains some data and bits of code.
If a customer want to occupy the special room, he has to enter the Hallway(Entry Set) to wait first. Scheduler will pick one based on some criteria(e.g. FIFO). If he is suspended for some reason, he will be sent to the wait room, and be scheduled to reenter the special time later some time.
In brief, a monitor is a facility which monitors the threads’ access to the special room. It ensures that only one thread can access the protected data or code.
2. How is it implemented in Java?
In the Java virtual machine, every object and class is logically associated with a monitor. To implement the mutual exclusion capability of monitors, a lock (sometimes called a mutex) is associated with each object and class. This is called a semaphore in operating systems books, mutex is a binary semaphore.
If one thread owns a lock on some data, then no others can obtain that lock until the thread that owns the lock releases it. It would be not convenient if we need to write a semaphore all the time when we do multi-threading programming. Luckily, we don’t need to since JVM will do that for us automatically.
To claim a monitor region which means data not accessible by more than one thread, Java provide synchronized statements and synchronized methods. Once the code is embedded with synchronized keyword, it is a monitor region. The locks are implemented in the background automatically by JVM.
3. In Java synchronization code, which part is monitor?
We know that each object/class is associated with a Monitor. I think it is good to say that each object is a monitor, since each object could have its own critical section, and capable of monitoring the thread sequence.
To enable collaboration of different threads, Java provide wait() and notify() to suspend a thread and to wake up another thread that are waiting on the object respectively. In addition, there are 3 other versions:
wait(long timeout, int nanos)
wait(long timeout) notified by other threads or notified by timeout. 
notify(all)
Those methods can only be invoked within a synchronized statement or synchronized method. The reason is that if a method does not require mutual exclusion, there is no need to monitor or collaborate between threads, every thread can access that method freely.
Here are some synchronization code examples.

No comments: