File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes calling wait() & sleep() on/off an object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "calling wait() & sleep() on/off an object " Watch "calling wait() & sleep() on/off an object " New topic
Author

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

jon ruane
Greenhorn

Joined: Mar 08, 2007
Posts: 13
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.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
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.


Rahul Bhattacharjee
LinkedIn - Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: calling wait() & sleep() on/off an object