What is the difference between sleep() and wait() methods in Thread in Java?

Answered

What is the difference between sleep() and wait() methods in Thread in Java?

Ninja Asked on 17th September 2018 in Java.
Add Comment
1 Answer(s)
Best answer

• Main difference between wait() and sleep() is that wait() method release the ownership of the acquired monitor when thread is waiting while Thread.sleep() method keeps the lock or monitor even if thread is waiting.

• The wait() method should be called from synchronized block while there is no such requirement for sleep() method.

• Another difference is Thread.sleep() method is a static method and applies on current thread, while wait() is an instance specific method and wakes up only if some other thread calls notify() method on same object.

• In sleep() method, sleeping thread immediately goes to Runnable state after waking up (after the time elapses) while in case of wait(), waiting thread first acquires the lock and then goes into Runnable state.

Ninja Answered on 17th September 2018.
Add Comment