Friday, March 8, 2013

difference between sleep() and wait() in Java

Both the sleep() and wait() methods are used to suspend a thread execution for a specified time.
When sleep() is executed inside a synchronized block, the object is still under lock .

When wait() method is executed,it breaks the synchronized block , so that the object lock is removed
and it is available.

Generally , sleep() is used for making a thread to wait for some time.But wait() is used in connection
with notify()  or notifyAll() methods in thread communication.

sleep method present in Thread class, wait method present in Object class.
sleep method is static , wait is non static method

No comments:

Post a Comment