Difference between wait and sleep in java

Before moving further we must have knowledge about the sleep() method and wait() method. In this post, we will read the difference between wait and sleep in java.

Difference between wait and sleep in java

Difference between wait and sleep in java

Defined in different classes

The sleep() method is defined in Thread class and it is a static method so we can call it by Thread class.

The wait() method exists in Object class and it is a non-static method so we can call it by the object.

Context

The wait() method is called only from the synchronized context that is synchronized block or synchronized method.
The sleep() method can be called from any context.

Call

As we discuss the wait() method is defined in Object class. So, it operates on Object and we can call the wait() method only by thread object which we want to make wait.

A sleep() method operates on current thread and when we call the sleep() method, the JVM assume we want to sleep the thread in which method is placed.  

State change

When we use the sleep() method, it pauses the execution of the current thread and moves that thread in TIMED_WAITING state. After the competition of waiting time, the thread moves again in RUNNABLE state.

Where wait() method also pauses the execution of the thread and move that thread in the WAITING state.

Lock

The sleep() method doesn’t release the lock on an object and it holds the lock for a specific time or until interrupt.

The wait() method releases the lock on an object and gives another thread a chance to acquire lock and enter a synchronized method or block.

Wake up

When a thread is paused, and it is in TIMED_WAITING state after calling sleep() method. It can’t be woken up. It wakes up when given time expires. If any thread interrupts in between it throws InterruptedException. 

When a thread is paused, and it is in WAITING state after calling the wait() method. it can be woken up by other threads by calling notify() or notifyAll() methods on the same lock.

Method available

The sleep() method is available in Thread class only. We can use the sleep() method by use of Thread class or we can extend the Thread class in own class.

The wait() method is available in the Object class. It means, the wait() method is available in all classes. We can call it by use of object of class.

Execution

If we call the sleep() method by use of thread object it will pause the current thread, not the thread that is using to call. Suppose if we are calling thread1.sleep(), some programmer misunderstood the concept. It will also pause the current thread.

In each class wait() method is available, so the object has each wait() method for inter-communication between threads.

Exception

The sleep() method throws InterruptedException if any thread has interrupted the current thread and IllegalArgumentException if the value of arguments is negative.

The wait() method throws exception If the current thread is not the owner of the object’s monitor then the compiler throws IllegalMonitorStateException.
If any thread interrupted the current thread before or while the current thread was waiting. then compiler throws InterruptedException.

Leave a Comment