In this post, we will understand the difference between sleep and wait methods in Java.
Wait
It belongs to the ‘Object’ class.
This method releases the lock when synchronization is in process.
It is not a static method.
It is to be called only from a synchronized context.
This method has three overloaded methods, namely
wait()
wait(long timeout)
wait(long timeout, int nanoseconds)
Sleep
It belongs to the ‘Thread’ class.
It doesn’t release the lock on the object when synchronization is in process.
It is a static method.
From a synchronized context, there is no need to call the ‘sleep’ method.
This method has three overloaded methods, they are −
sleep(long millis)millis: milliseconds
sleep(long millis,int nanos) nanos: Nanoseconds