What is the Difference Between wait and sleep Methods in Java ?

The main difference between wait() and sleep() is that wait is an Object level method, whereas sleep() is a static method in Thread class. A waiting thread can be woken up by another thread by calling notify() on the monitor which is being waited on. But a sleeping thread cannot be woken up.

A wait() and notify() has to happen within the same block that is synchronized on the monitor object.

When we call wait() the current thread releases the monitor and goes to waiting state. Then another thread calls notify() to wake it up.

In case of sleep() current thread does not release the monitor or locks. It just sleeps for some pre-defined time period.



You May Interest

What is the Difference Between a Nested Class and an Inner Class ...

What are the Similarities Between HashSet and HashMap in Java ?

What are the Differences Between a Vector and an ArrayList in Jav ...

How remove Method is Implemented in a HashMap in Java ?

What are the Differences Between a HashSet and a HashMap in Java ...