Difference between wait() and sleep() in java - Java @ Desk

Monday, June 3, 2013

Difference between wait() and sleep() in java

1) sleep() method is called on a Thread while wait() method is called on object

2) wait() method is called from a synchronized method or block while sleep() has no such restrictions.If you call wait method without synchronization, it will throw IllegalMonitorStateException in Java

3) sleep() is a static method of Thread class and it is applicable to current running thread while wait() is an instance method of Object class.

4) When a sleep() method is called on a thread then it immediately goes into a runnable state while in case of wait() it is required to call notify() or notifyAll() method in order to release a lock and make a thread enter into runnable state.

5) Thread in a waiting state can be released by calling a notify() or notifyAll() method while sleep() does not have such feature. A thread in sleep() mode remains silent for the specific amount of time.

Other references on threads
Difference between sleep and wait in java
Lifecycle of thread
Difference between yield and sleep in java
Thread creation in java







No comments:

Post a Comment