• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Santiago Bravo
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
aaah (lightbulb)

thanks alot Ankit, makes sense now
 
Santiago Bravo
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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)
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
fantastic, thanks for making it clearer
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()
 
Well behaved women rarely make history - Eleanor Roosevelt. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic