*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes sleep( ) and wait( ) 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 "sleep( ) and wait( )" Watch "sleep( ) and wait( )" New topic
Author

sleep( ) and wait( )

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi, if someone can explain the exact difference between the
sleep() and wait() methods of threads..
Wali
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
hi,
in sleep() a thread "sleeps" for the specified amount of time and it keeps the locks on any object that it currently holds.
in wait() the thread "waits" until another thread notifies it useing notify or notifyAll() to wake up. in this case the object relenquishes control over any locks that it currently holds.
Rahul
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Rahul, what you said is as such correct. Also there is also 2 more wait(long timeout) and wait(long timeout, int nanosec) types. These 2 types are used for the safer side. What if Nobody notifies this waiting thread at all ? It has to wait forever right? So in order to just wait for some time, if someother thread notify()/notifyAll() it is ok. If not, after the specified amt of time is elapsed, it is taken out of the wait set of the object, for whose lock it is waiting for by means of some other thread notify()/notifyAll(), it goes straight to ready state
regds
maha anna
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks Rahul and Maha
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
fyi
sleep() is a static mehtod in the Thread class
* put the current thread to sleep for a period of time
the thread is in the wait state. after sleeping, the thread
is moved from a wait to a ready state.
yield() is a static method in the Thread class
* moves the current thread from a runing state to a ready state
wait() is a method in the Object class
* moves the current thread into a wait state until ..
notify() notifyall() interrupt() or System.exit()
notify() is a method in the Object class
* moves one thread from a wait to a seeking lock state
once a lock is obtained, by one thread choosen at random
the thread will be moved into a ready
notifyall() is a method in the Object class
* moves one or more thread(s) from a wait to a seeking lock state
once a lock is obtained, by one thread choosen at random
the thread will be moved into a ready
let me know if i have missed anything,
Monty
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Another related method .....
suspend() is a (depricated) method of the thread class.
This will also stop a thread from running. But the
important point is it can be resume()d by another thread.
suspend() is not a permenant way to stop a thread.
(and who says deprecated methods are not asked ... )
Regds.
- satya

[This message has been edited by satya5 (edited June 08, 2000).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sleep( ) and wait( )
 
Similar Threads
wait() vs sleep()
Thread
Sleeping thread takes locks with it?
About Object lock
Q on thread at Jxam