aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes wait() not syncronized on this but not throwing illegalMonitorStateException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "wait() not syncronized on this but not throwing illegalMonitorStateException" Watch "wait() not syncronized on this but not throwing illegalMonitorStateException" New topic
Author

wait() not syncronized on this but not throwing illegalMonitorStateException

Thiyagarajan Kamala
Ranch Hand

Joined: Mar 16, 2003
Posts: 30



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

Joined: Aug 03, 2008
Posts: 9293
    
  17

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)...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Thiyagarajan Kamala
Ranch Hand

Joined: Mar 16, 2003
Posts: 30
Basically wait() is this.wait() you are calling on this.Shouldn't it be synchronized on this






Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Look at the method declaration, it is synchronized



is similar to

Anastasia Sirotenko
Ranch Hand

Joined: Jul 20, 2009
Posts: 64
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


[SCJP 6.0]
Thiyagarajan Kamala
Ranch Hand

Joined: Mar 16, 2003
Posts: 30
Thanks for the explanation.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

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


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

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.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Thiyagarajan Kamala
Ranch Hand

Joined: Mar 16, 2003
Posts: 30



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

Joined: Jul 20, 2009
Posts: 64
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

Joined: Mar 16, 2003
Posts: 30
Thanks Anastasia I increased the sleep time and saw the difference.
Anastasia Sirotenko
Ranch Hand

Joined: Jul 20, 2009
Posts: 64
Thiyagarajan Kamala wrote:Thanks Anastasia I increased the sleep time and saw the difference.

thank you too, it was a nice example for practicing
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wait() not syncronized on this but not throwing illegalMonitorStateException