Monday, 16 January 2017

What is difference in wait() and sleep() ?

Difference in wait() and sleep()

Wait()
Sleep()
Can be woken up by another process calling notify() or notifyall() on the monitor
Cannot be woken up
wait (and notify) must happen in a block synchronized on the monitor
Does not
Called on a Object
Called on Thread
waiting releases the lock on the object
sleeping a Thread does not release the locks it holds
wait() is used for multi-thread-synchronization.
sleep()is used for time-syncronization
In sleep() the thread stops working for the specified duration
In wait() the thread stops working until the object being waited-on is notified

No comments:

Post a Comment