aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Calling wait()/ notify() in run() 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 "Calling wait()/ notify() in run()" Watch "Calling wait()/ notify() in run()" New topic
Author

Calling wait()/ notify() in run()

Santiago Bravo
Ranch Hand

Joined: Jul 25, 2008
Posts: 226
Hi All,

I have the following code:



When I run the above code I get no output. If I change Line 1 to synchronize on "this":



or change Line 2 to:




I get the desired output - "Running".

What is happening in the original code and why does the print statement not execute? Is this due to the lock on the object being
synchronized which has to be called such as s.wait(5); instead of just wait(5); ?


Second question is, why can't we call wait(), notify(), notifyAll() in a static method? is this because there is no "this" in a static method?

Thanks


Santiago
My Path to SCJP Certification My Path to SCWCD Certification
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

In the original code when you call wait, it is a call to wait on the this reference. Since the call to wait is not in synchronized context of this, so a IllegalMonitorStateException is thrown. Since the associated catch block does nothing when an exception is raised, so you see no output.

You are right that calling s.wait(5); will solve the problem as the code is synchronized on s. But that is not a perfect solution. The call to wait will return only because of timeout i.e. the 5 milliseconds will expire. The call will not return if you write s.wait() as no one notifies it.

So the correct code would be something like this



you cannot call wait, notify and notifyAll in static methods because they are non-static methods in Object class and you need the this reference (which is like an object not a method) to access them...

[Edit: Removed the logical error that Santiago and Srilatha are referring below in this thread]
[ December 03, 2008: Message edited by: Ankit Garg ]

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Santiago Bravo
Ranch Hand

Joined: Jul 25, 2008
Posts: 226
aaah (lightbulb)

thanks alot Ankit, makes sense now
Santiago Bravo
Ranch Hand

Joined: Jul 25, 2008
Posts: 226
Actually I do have a query about:



Is this being called in a synchonized context? Is this being achieved by the synchronized object 's'?


Thanks
M Srilatha
Ranch Hand

Joined: Aug 27, 2008
Posts: 137
Is this being called in a synchonized context? Is this being achieved by the synchronized object 's'?


No this call is not in synchronized context. Hence it will throw runtime exception when you execute. (java.lang.IllegalMonitorStateException)


Thanks,<br />Srilatha M
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Originally posted by M Srilatha:


No this call is not in synchronized context. Hence it will throw runtime exception when you execute. (java.lang.IllegalMonitorStateException)


Oops! An error in my program . I am correcting it in the original post...
Santiago Bravo
Ranch Hand

Joined: Jul 25, 2008
Posts: 226
fantastic, thanks for making it clearer
meera kanekal
Ranch Hand

Joined: Feb 13, 2005
Posts: 75
I just want to clarify a point. When you have a class that extends Thread class, and when the start() is invoked does it automatically invoke the overridden run() ?
Thanks,
Meera
Santiago Bravo
Ranch Hand

Joined: Jul 25, 2008
Posts: 226
Hi Meera,


Thread already implements Runnable


http://java.sun.com/j2se/1.5.0/docs/api/

So it implements the Runnable interface method run() but actually does nothing if you dont override the Thread run() method

Any subclass of Thread should override run()
 
 
subject: Calling wait()/ notify() in run()