Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

objects & monitors  RSS feed

 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is an object a monitor? Or does an object have a monitor?
Here are some statements from a specification intended to replace chapter 17 of the JLS.

Each object in Java is associated with a monitor, which a thread can lock and unlock.
Only one thread at a time can hold a lock on a monitor.
Any other threads attempting to lock that monitor are blocked until they can obtain a lock on a monitor.

I thought, a monitor is a module or an object with condition variables and two operation on them, wait and signal. Only one operation of a given monitor is allowed to be active at a given point in time.
A Java object has a wait set and two operations, wait and notify/notifyAll. A Java object can serve as a monitor.
Why do they say an object is associated with a monitor? What is the monitor if it is not the object?
JSR-133: Community Review Document
[ December 26, 2003: Message edited by: Marlene Miller ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it's a bit uncertain, but maybe monitors can be implemented as separate objects. They might have some space allocated for info such as which thread currently has a sync lock, and which other threads are waiting on the monitor, and which are trying to acquire the monitor. Such a structure needn't be created for every object in the JVM - instead it can be created on demand for any object which is synced on. Note that the monitor may not even be a Java object at all, strictly speaking - it may be something implemented entirely in native code. I think that the new spec is written this way to allow for the possibility that a monitor can be to some extent be thought of as separate from the associated object. I suspect though that they could have phrased this either way. But conceptually, a "monitor" represents a set of behavior that we don't really need or care about for most objects. We only need to think about it if an object is named as the target of a synchronized(), wait() or notify[All](). However exactly it's implemented, that's a JVM implementation decision - but conceptually it's a bit easier for us to think of monitors as something a bit different from a regular object.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Jim.
[ December 26, 2003: Message edited by: Marlene Miller ]
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In any event their is a one to one relationship between objects and monitors.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!