aspose file tools*
The moose likes Threads and Synchronization and the fly likes wait() and sleep() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "wait() and sleep()" Watch "wait() and sleep()" New topic

wait() and sleep()

Vallabhaneni Suresh Kumar
Ranch Hand

Joined: Mar 01, 2004
Posts: 68
What is the difference between wait and sleep and why is it important?

Thank you
pankaj patil
Ranch Hand

Joined: Dec 19, 2004
Posts: 98
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 Patil
Sravan Kumar
Ranch Hand

Joined: Sep 11, 2005
Posts: 121
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.

keep smilin :: sravan<br /><a href="" target="_blank" rel="nofollow">I scribble here</a>
rekha devan

Joined: Oct 10, 2005
Posts: 26
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.

Jason Bourne

Joined: Oct 06, 2005
Posts: 9
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.
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

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.
Abhay S Dandekar

Joined: Dec 29, 2008
Posts: 4
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 ...
Abhay Dandekar
I agree. Here's the link:
subject: wait() and sleep()