• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

wait() not syncronized on this but not throwing illegalMonitorStateException

 
Thiyagarajan Kamala
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



The above snippet of code doesnt throw illegalMonitorStateException eventhough the method is synchronized the obj on which wait() is called is not synchronized .Why it is not throwing exception.
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why will it throw an exception. The method itself is synchronized. Suppose this method is called on an object obj, then the method will obtain a lock on that object and wait() will also be called on that object i.e. obj (or this inside the method)...
 
Thiyagarajan Kamala
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically wait() is this.wait() you are calling on this.Shouldn't it be synchronized on this






 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the method declaration, it is synchronized



is similar to

 
Anastasia Sirotenko
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thiyagarajan Kamala wrote:Basically wait() is this.wait() you are calling on this.Shouldn't it be synchronized on this



synchronized modificator for a non-static method synchronize on the instance of the class itself, on this object, so

is the same as
 
Thiyagarajan Kamala
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:Look at the method declaration, it is synchronized



is similar to



Precisely.

You might also want to think about what happens when the method is static
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well here the important to be noted is its not important for a snchronized block to be present whenever a wait is called but to acquire the lock on the object on which the wait was called.
 
Thiyagarajan Kamala
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



I did a small change to wait to synchronize on class level but the notification is not happening.Kind of confused ...asusual on this things......
 
Anastasia Sirotenko
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thiyagarajan Kamala wrote:
I did a small change to wait to synchronize on class level but the notification is not happening.Kind of confused ...asusual on this things......


if you will put some output in the block where you notifying, you will see that this block realy runs before the waiting block runs. So you first notify on object and then lock and wait on it. You put 1st thread to sleep 1000ms, then 2nd thread runs a bit and goes sleep 1000ms, whilst 1st wakes first and does it's notify evil thing. After that 2nd thread wakes and procedes to its wait, but in vain, no notifies left there for him.

 
Thiyagarajan Kamala
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Anastasia I increased the sleep time and saw the difference.
 
Anastasia Sirotenko
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thiyagarajan Kamala wrote:Thanks Anastasia I increased the sleep time and saw the difference.

thank you too, it was a nice example for practicing
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic