One of my favorite interview question is: What is the difference between sleep() and wait() method? The only commonality between these two method is both halts the current thread execution. The purpose and usage are entirely different.
Object.wait() method:
a) This method available in Object.class
b) It can be used only inside the synchronized method or block.
c) It halts the current thread execution and releases the lock of the object. Hence other thread can use the object for execution.
d) Wait state can be terminated by calling Object.notify() method.
e) Wait() and notify() methods are specific to an instance of an object. (Not at the class level)
Thread.sleep() method:
a) This method available in Thread.class.
b) It can be used anywhere in the code to halt the current thread execution for the specified time.
c) The sleep state can be terminated by invoking interrupt() method of the thread instance.
Thread.sleep() is most frequently used method. Object.wait() is used in concurrent thread access codes only.

Leave a comment