• Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronization in Concurrent Programming Monitor  RSS feed

 
Shekar Chandu
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ,

I am reading threads and was having questions on term"Every Object has a monitor".After googling i undertood this.

There  are multiple ways we can achieve concurrency

1)Semaphore
2)Monitors
3)Message Passing

In order to protect shared resources from multiple Threads Java uses Monitor Model.

In Monitor model of concurrent programming ,Monitor encapsulates shared data and operation that operate on it and thus behaves as a data type, and
Monitor=MUTEX+CO-ORDNATION.

When compared with Java Object Oriented Language,MUTEX is acived by using synchronized keyword with method and CO-ORDNATION with wait-notify.

In java since a class encapuslates data and methods that operates on  data and when we compare this with MOnitor model,every object of the class if it has synchronized method acts as Monitor.

And becuase of this JVM differentiates between normal objects and MOnitor Objects and thus apply locks correpsondingly.

Is my understanding correct?
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shekar Chandu wrote:
There  are multiple ways we can achieve concurrency

1)Semaphore
2)Monitors
3)Message Passing

In order to protect shared resources from multiple Threads Java uses Monitor Model.


The core Java API actually supports all three that you mentioned. If you want semaphores, take a look at the java.util.concurrent.Semaphore class. If you want a collection that is very good at message passing between producers and consumers, take a look at the many implementations of the java.util.concurrent.BlockingQueue interface.

Henry
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shekar Chandu wrote:
In Monitor model of concurrent programming ,Monitor encapsulates shared data and operation that operate on it and thus behaves as a data type, and
Monitor=MUTEX+CO-ORDNATION.


Correct. In Java, any object can be used as both a mutex and a condition variable.

However, this is arguably not very flexible, as there may not be a one to one relationship between a mutex and a condition variable. So, the core Java library also implement mutex and condition variables as a Java class that can be used. See the ReentrantLock class and the associated Condition instance that can be created from that class.

Henry
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!