Monitor in Java

Monitor in Java

Monitor is a synchronization construct that allows threads to have both mutual exclusion and the ability to wait (block) for a certain condition to become true. It is also known as intrinsic lock.

Monitors also have a mechanism for signaling other threads that their condition has been met. It is an entity that possesses both a lock and a wait set. In Java, any Object can serve as a monitor.

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 terms, 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.

Each object/class is associated with a Monitor. It’s good idea to say that each object has 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.

  • Add synchronized keyword to instance method 


  • Add synchronized block


This gains the lock on object monitor and does not allow the other threads to concurrently access the piece of code which is being synchronized. Once the thread which had the monitor come out of the synchronized code, it releases the lock on object monitor and other threads waiting to gain access to the synchronized code will gain the lock.

To know about multi threading refer the link.

Happy Learning!!



Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Notify of