DEV Community

Pramod Bablad
Pramod Bablad

Posted on

wait() Vs sleep() Methods In Java

wait() :

1) The thread which calls wait() method releases the lock it holds.
2) The thread regains the lock after other threads call either notify() or notifyAll() methods on the same lock.
3) wait() method must be called within the synchronized block.
4) wait() method is a member of java.lang.Object class.
5) wait() method is always called on objects.
6) wait() is a non-static method of Object class.
7) Waiting threads can be woken up by other threads by calling notify() or notifyAll() methods.
8) To call wait() method, thread must have object lock.

sleep() :

1) To call wait() method, thread must have object lock.
2) No question of regaining the lock as thread doesn’t release the lock.
3) sleep() method can be called within or outside the synchronized block.
4) sleep() method is a member of java.lang.Thread class.
5) sleep() method is always called on threads.
6) sleep() is a static method of Thread class.
7) Sleeping threads can not be woken up by other threads. If done so, thread will throw InterruptedException.
8) To call sleep() method, thread need not to have object lock.

See more at https://javaconceptoftheday.com/difference-between-wait-and-sleep-methods-in-java/

Top comments (0)