views:

187

answers:

4
+12  Q: 

what's a monitor?

What's a monitor referred to the concurrent programming in Java?

When I read that "every object has associated a monitor" what does it meaning?

Is it a special object?

+9  A: 

A monitor is mechanism to control concurrent access to an object.

This allows you to do:

Thread 1:

public void a()
{
    synchronized(someObject) {
        // do something (1)
    }
}

Thread 2:

public void b()
{
    synchronized(someObject) {
        // do something else (2)
    }
}

This prevents Threads 1 and 2 accessing the monitored (synchronized) section at the same time. One will start, and monitor will prevent the other from accessing the region before the first one finishes.

It's not a special object. It's synchronization mechanism placed at class hierarchy root: java.lang.Object.

There are also wait and notify methods that will also use object's monitor to communication among different threads.

Pablo Santa Cruz
so can we say that when we create a synchronized method we are defining a lock (monitor) on that object's method?
xdevel2000
Exactly. When you hace a synchronized method (which I didn't put in my answer) you are creating a monitor on that method.
Pablo Santa Cruz
Erm, not exactly. Each object automatically has a monitor (mutex) associated with it, regardless of anything else. When you declare a method synchronized, you're declaring that the runtime must obtain the lock on the object's monitor before execution of that method begins (and must release the lock before control returns to the calling code).
Andrzej Doyle
And @Pablo - there's no such thing as a monitor for a method; monitors only exist for objects, which would be the enclosing instance for most methods, or the corresponding `Class` object for static methods. If you already have a synchronized method1() and you declare method2() synchronized, no new monitors are created and in fact invoking either method (on the same object) will attempt to lock the same monitor. This often catches out newcomers.
Andrzej Doyle
it's Operating Systems class all over again...minus the night terrors...
rownage
@Andrzej: I see... OK. Will fix that in the answer. Thanks!
Pablo Santa Cruz
@Andrzej: so, every object has ONE monitor associated with it. Then I can have many synchronized methods. After whenever of that methods a thread call it obtain that monitor that do the sync stuff.
xdevel2000
A: 

A monitor is a essentially a class where all of the methods are mutually exclusive.

http://en.wikipedia.org/wiki/Monitor_%28synchronization%29

Joel
+1  A: 

http://java.sun.com/docs/books/jvms/second_edition/html/Concepts.doc.html#33308

A mechanism to control access to objects one at a time

naikus
+3  A: 

A monitor is an entity that possesses both a lock and a wait set. In Java, any Object can serve as a monitor.

For a detailed explanation of how monitors work in Java, I recommend reading the Monitor Mechanics section of Concurrent Programming in Java (the preceding link displays the preview in Google books, and that section is available for reading).

JRL