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

wait() and sleep()

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the difference between wait and sleep and why is it important?

Thank you
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wait(300) and sleep(300) are the method in thread that make the thread to wait for 300 second. Difference is that the notify() and notifyAll() methods can be used to make the hread to come out of the waiting state in the case of wait .


after the time expires the thread goes to ready aste in case of wait() and thread goes to waiting state in case of sleep()

with regards
Pankaj
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The sleep() method is static. It makes the current thread go to a non-runnable (generally called sleeping) state. The thread is guaranteed not to run atleast for the milliseconds passed as argument to this method.

The wait() method is instance-specific and is invoked on an object. It should be called only from within a static context (static method or a block) and the thread that invokes this method on the object will not run unless it has the object's lock. [Every object has a lock]. For this thread to continue execution, notify() or notifyAll() has to be called on the same object by some other thread that has the lock. After it gets a notify, the thread goes to runnable state.

after the time expires the thread goes to ready aste in case of wait() and thread goes to waiting state in case of sleep()



A thread goes to runnable state from both blocked/sleeping state.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to add one more point.
The Thread goes to ready-run state and then to runnable state after the times specified in the sleep()method elapses.

whereas in case of wait() method after after receiving notification it will have to wait to acquire the lock on the object. Then goes to ready- run state and thereby to running state.

Rekha
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, use sleep(msec) when you know for sure that you wont need the application to run for that period (at the least). Note that this period could extend. This can be used in animations or games without much interaction, timers, in particular.

wait() is used when you wont need the object till a particular action or event takes place. And a waiting thread could be woken immediately or never be.
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't mean to be funny, but try looking it up in a dictionary. The difference between the english words gives you a good indication of the difference between the programming concepts.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thread.sleep() sends the current thread into the "Not Runnable" state for some amount of time. The thread keeps
the monitors it has aquired -- i.e. if the thread is currently in a synchronized block or method no other thread
can enter this block or method. If another thread calls t.interrupt() it will wake up the sleeping thread.
Note that sleep is a static method, which means that it always affects the current thread (the one that is
executing the sleep method). A common mistake is to call t.sleep() where t is a different thread; even then, it is
the current thread that will sleep, not the t thread.

object.wait() sends the current thread into the "Not Runnable" state, like sleep(), but with a twist. Wait is
called on a object, not a thread; we call this object the "lock object." Before lock.wait() is called, the
current thread must synchronize on the lock object; wait() then releases this lock, and adds the thread to the "wait
list" associated with the lock. Later, another thread can synchronize on the same lock object and call lock.notify().
This wakes up the original, waiting thread. Basically, wait()/notify() is like sleep()/interrupt(), only the active
thread does not need a direct pointer to the sleeping thread, but only to the shared lock object.

Hope this clarifies all the doubts ...
Regards,
Abhay Dandekar
 
reply
    Bookmark Topic Watch Topic
  • New Topic