Wednesday, March 2, 2016

Difference between wait and sleep methods

1) wait() method must be called from synchronized context i.e. from synchronized method or block in Java. If you call wait method without synchronization, it will throw IllegalMonitorStateException in Java.

2) wait() method operates on Object and defined in Object class while sleep operates on current Thread and defined in java.lang.Thread class.

3) wait() method releases the lock of object on which it has called, it does release other locks if it holds any while sleep method of Thread class does not release any lock at all.

4) wait() is a non static method while sleep() is static method in Java.

No comments: