• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting monitor of an object?  RSS feed

 
manish ghildiyal
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am a bit confused about how synchronization keyword works regarding acquiring lock.
In my method if I do like this,



....so it means that synchronized block has the object ,on which method is called ,as monitor. Hence we can call
wait() method on that object. But if instead I use some other object as monitor then I suppose that I can't call wait() on current object
as now thread gets lock on that monitor object and not on the current object.
But what I see is that I am still able to call wait on current object while I was expecting IllegalMonitorStateException.

Where is my understanding wrong?

Manish

 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you call "wait()" without specifying on which object, it applies implicitly to the same "this", against which the block is synchronized.

Or did you describe the behaviour of another code snippet?
 
manish ghildiyal
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ivan, thanks.

I was wrong in my understanding, now its clear to me.

Manish
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!