What is a monitor in concurrent programming in Java?

You might come across the questions like What is a monitor referred to the concurrent programming in Java? or the term which is often used "every object has associated a monitor", what does it mean?

What is monitor in java?

In concurrent programming, a monitor is an object or module intended to be used safely by more than one thread. In simple words we are synchronizing the threads or it’s a mechanism developed by the object to control access to objects one at a time.
So when we consider Java as a programming language , any Object can serve as a monitor. Java’s monitor supports two types of thread synchronization

1. Mutual Exclusion
2. Co-operation

Mutual Exclusion and Monitor in java

Java Virtual Machine supports mutual exclusion using object locks which allows multiple threads to independently work on shared data without interfering with each other. In case of mutual exclusion only one thread can share or execute the monitor object code and other thread waits for first thread to complete.

Co-operation and Monitor in java

Java Virtual Machine supports co operation with the help of wait and notify method of class Object. Basically it provides a mechanism for threads to temporarily give up exclusive access by waiting for some condition to be met. Such as wait and notify methods allow mechanism for signaling other threads that particular conditions have been met. This form of monitor used in java by Java Virtual Machine is also called a Wait and Notify Monitor or Signal and continue monitor. In this Wait and Notify monitor, a thread which currently owns a monitor object can suspend itself inside the monitor with the help of Wait method what it do is it releases the monitor object and enters in a wait state. The wait state ends when another thread call notify method inside the monitor object. When any thread calls notify it owns that monitor object until it releases the monitor using wait object or done with the operation on that monitor object region.
Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment


Post a Comment