File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
jon ruane
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic