views:

160

answers:

3

Hi,

What is the difference between wait() and wait(timeout).Anyway wait() needs to wait for a notify call but why we have wait(timeout) ?

So what is the difference between sleep(timeout) and wait(timeout) ?

+5  A: 

wait(timeout) will return if the thread is still waiting after the timeout has elapsed. This is for hang notification, for low-power polling, etc etc. Sleep(timeout) won't wake up until the timeout has elapsed; wait(timeout) is either the notify() call or the timeout, whichever comes first.

Quoting from the JavaDoc:

"This method causes the current thread (call it T) to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object. Thread T becomes disabled for thread scheduling purposes and lies dormant until one of four things happens:

* Some other thread invokes the notify method for this object and thread T happens to be arbitrarily chosen as the thread to be awakened.
* Some other thread invokes the notifyAll method for this object.
* Some other thread interrupts thread T.
* The specified amount of real time has elapsed, more or less. If timeout is zero, however, then real time is not taken into consideration and the thread simply waits until notified."
Borealid
Just so it's said...sleep(timeout) will wake up if the thread's interrupted. But then, any of the others should too.
cHao
Also important to note is that sleep(timeout) does not require synchronization, wait(timeout) does.
Tim Bender
+1  A: 

The only difference between wait() and wait(timeout) is that wait() will never wake up without a notify(). wait(timeout) with a timeout > 0 allows you to potentially save yourself from locking up your application "forever" if a call to notify() never occurs.

MKA
So sleep(timeout) and wait(timeout) are the same!!!???
JavaUser
The main difference between those two methods is that wait releases ownership of the object's monitor while sleep doesn't. For example, let's say an object has two synchronized methods A() and B(). If you call wait(timeout) in A(), another thread can call B() while the other thread is waiting. But if you call sleep(timeout) in A(), the other thread can't call B() until that sleep is over and A() is finished (since the lock on the object's monitor is not released while sleeping).
MKA
No. Read either my answer or xagyg, which both explain the difference.
Borealid
Yes. Please refer to the Java API:Object.wait(long timeout):"The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution."Thread.sleep(long millis):"The thread does not lose ownership of any monitors."What you and xagyg posted is also correct, though. :)
MKA
+2  A: 

wait(timeout): when the timeout expires the thread wakes up and tries to re-acquire the synchronization lock (i.e. if another thread has not notified it within the timeout period).

sleep(timeout): sleep can be used without any sycnhronization code - it just puts the thread to sleep for the specified timeout. Note that wait must be used within synchronized code.

So, wait is used when you expect a thread to be notified by another thread (but may not, hence timeout). And, wait must be called within synchronized code.

xagyg
+1 for explaining the use case semantics of wait v. sleep
Tim Bender