• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Synchronization in Concurrent Programming Monitor

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic