Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sleep( ) and wait( )

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, if someone can explain the exact difference between the
sleep() and wait() methods of threads..
Wali
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rahul and Maha
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic