[Logo]
Forums Register Login
difference between wait() and sleep()
Hi,

what is the difference between wait() and sleep() in threads.
both methods make the thread into waiting state. but the difference is that on call of wait() the thread releases the lock on the object it has acquired whereas sleep() doesnt do.
Another difference is that a thread executing the Thread.sleep method cannot
be awaken, whereas a thread executing the Object.wait() can be awaken by
another thread invoking the Object.notify() method.
On the other hand, both methods can throw an InterruptedException is the thread executing the method is interrupted.

Gilles
Perfect..
The main difference between two method is that sleep is a static method of class Thread and wait is method in Object class

Thread.sleep()
wait() non static method in Object class
 
Originally posted by kiran peruka:
Hi,

what is the difference between wait() and sleep() in threads.


One more and main difference between wait() and sleep() is that wait() can be only used from synchronized block and main purpose of wait() is inter thread
communication.
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards



All times above are in ranch (not your local) time.
The current ranch time is
Nov 18, 2017 16:46:55.