• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

calling wait() & sleep() on/off an object

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i know how to use the wait() and sleep() methods, but one thing about them is puzzling me:

under what circumstances would you call these methods on an object, as oppose to just calling them on their own?

for example iv seen code like this:

object.wait();
thread.sleep();

but also:

wait();
sleep();

i know that wait() must be done within a sync block, so is wait() actually looking at what the block is sync'd on, and thats the object it sets to waiting?

many thanks to anyone who can clear this little query up.

john.
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by jon ruane:


for example iv seen code like this:

object.wait();
thread.sleep();

but also:

wait();
sleep();



Hey John:

sleep is a static method in Thread , so no need to call it upon a Thread object.Though its not wrong (will compile) and will have the same effect , that is putting the current thread to sleep for desired amount of time.

For wait I think the only thing necessary is that you should call it from a syncronized context , that is you have to aquire a lock before you call it.
As its a public method in Object class , you can call it from the code directly or using new Object().wait();

Only thing is that the current thread would go to the wait set of the object whose lock you have acquired.
 
Do not set lab on fire. Or this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic