Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the monitor in Java multi-thread programming?

 
Tejas Jain
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can we handle it directly? I did not see a Monitor class in Java.
 
Paul Clapham
Sheriff
Pie
Posts: 20758
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20758
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20758
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic