jQuery in Action, 2nd edition*
The moose likes Threads and Synchronization and the fly likes What is the monitor in Java multi-thread programming? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "What is the monitor in Java multi-thread programming?" Watch "What is the monitor in Java multi-thread programming?" New topic
Author

What is the monitor in Java multi-thread programming?

Tejas Jain
Ranch Hand

Joined: Mar 04, 2008
Posts: 119
How can we handle it directly? I did not see a Monitor class in Java.


"Knowing is not enough, you must apply... Willing is not enough, you must do."
--Bruce Lee
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

The "monitor" is any object. When you say "handle it directly", what kind of "handling" did you have in mind? Normally all you do is to call methods like wait() and notify() on the monitor. There isn't anything more direct than that.
Tejas Jain
Ranch Hand

Joined: Mar 04, 2008
Posts: 119
Paul Clapham wrote:The "monitor" is any object. When you say "handle it directly", what kind of "handling" did you have in mind? Normally all you do is to call methods like wait() and notify() on the monitor. There isn't anything more direct than that.


Here is the link: http://www.artima.com/insidejvm/ed2/threadsynch.html. You can find:

"
Java's monitor supports two kinds of thread synchronization: mutual exclusion and cooperation. Mutual exclusion, which is supported in the Java virtual machine via object locks, enables multiple threads to independently work on shared data without interfering with each other. Cooperation, which is supported in the Java virtual machine via the wait and notify methods of class Object, enables threads to work together towards a common goal.
"

How can Monitor be any object?


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Really, Todd, if you have a question about what Bill Venners wrote, you should ask him that question.

But it's helpful that we now have a background for your question. That might make it easier to answer. But the other thing we need is background on you. I get the impression that you're a beginner in the threading area. If that's the case then you are better off not reading detailed technical descriptions of how the JVM takes care of synchronization under the covers. You aren't going to understand the concepts if you don't have any basic framework to put those concepts into.
Tejas Jain
Ranch Hand

Joined: Mar 04, 2008
Posts: 119
Actually I'm not new in Java, even multiple threading. I started using Java since 1.0 release.

I was interviewed recently and asked a question: What is Java monitor?

I think I know what the monitor does, but could not come up with a nice clean answer.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I see. But I wouldn't consider a four-page web article a "nice clean answer" either. Way too much detail. On the other hand "It's any object" probably isn't enough detail for an answer to an interview question. But really it's true. Any object can be a monitor for Java synchronization, so if you're just asked plain old "What is a monitor" then -- answering the question with an answer of the same level of detail -- you could say "It's any Java object". The follow-on questions might go into more detail.
 
 
subject: What is the monitor in Java multi-thread programming?
 
Similar Threads
WA #1.....word association
Monitor ???
Java application performance testing
Monitoring software for JBoss performance in linux
Java Synchronization and Monitors