Signup/Sign In
LAST UPDATED: AUGUST 5, 2021

Difference Between Wait and Sleep in Java

Wait and Sleep will come up quite frequently when working with threads. These terms are not interchangeable and have different meanings. Both of them are used to pause the execution of threads but there are a few major distinctions between them. Let's learn more about the differences between Wait and Sleep.

Sleep vs wait

Wait vs Sleep in Java

  • Both Wait and Sleep are used to pause the execution of a thread and take it into a non-runnable state. But in the case of sleep, the thread will wake up after a fixed time period. When the wait() method is used, then the thread can only be woken up when the notify() or the notifyAll() methods are called for the synchronized block that the thread is waiting on.
  • We will use sleep when we are just working with a single thread and want to stop its execution for some time. Whereas, the wait method is always used for multi-threading and synchronization. The wait method should always be called inside a synchronized block.
  • Another major difference between the two is that wait will release the lock but sleep will not. Because of this reason sleep is not used for synchronization purposes as a thread can sleep for a long duration and no other thread can gain access to the critical section.

The above points summarize the key differences between wait and sleep methods in Java. The following table discusses a few more differences.

Wait Sleep
The wait() method is called on an object. The sleep() is called on the currently executing thread.
Used for thread synchronization. Used for time synchronization.
The Wait() is not a static method. The sleep() is a static method.
The Wait() is only called from the synchronized block. If we call it from outside a synchronized block then we will get an IllegalMonitorStateException. The sleep() can be called outside a synchronized block as well.

The wait() can be used with the help of the following overloaded methods:

  • wait()
  • wait(long timeout)
  • wait(long timeout, int nanoseconds)

The sleep() can be used with the help of the following three overloaded methods:

  • sleep(long milliseconds)
  • sleep(long milliseconds, int nanoseconds)

Method Signature for sleep()

public static void sleep(long milliseconds) throws Interrupted_Execption

Method Signature for wait()

public final void wait(long timeout)

Example: sleep() and wait() in Java

Let's try to understand the working of the sleep() and wait() method with the help of the following example.

In the following example, the main thread will sleep for 1 second and then continue its execution. But when wait() is called then the thread waits infinitely as no other thread is calling notify() on the object. The program will keep running indefinitely.

public class SleepvsWait
{
	private static Object o = new Object();	
	public static void main(String[] args) throws InterruptedException
	{
		System.out.println("Thread Going to sleep...");
		Thread.sleep(1000);
		System.out.println("Thread woke up after 1 second.");
		
		synchronized(o)
		{
			System.out.println("Thread will wait for notify...");
			o.wait();
		}
		
		System.out.println("Thread woken up after notify() or notifyAll() or timeout.");
	}
}


Thread Going to sleep...
Thread woke up after 1 second.
Thread will wait for notify...

Now, let's specify a timeout duration for the wait() method. The thread will resume its execution after the timeout.

public class SleepvsWait
{
	private static Object o = new Object();
	public static void main(String[] args) throws InterruptedException
	{
		System.out.println("Thread Going to sleep...");
		Thread.sleep(1000);
		System.out.println("Thread woke up after 1 second.");
		
		synchronized(o)
		{
			System.out.println("Thread will wait for notify...");
			o.wait(1000);
		}
		System.out.println("Thread woken up after notify() or notifyAll() or timeout.");
	}
}


Thread Going to sleep...
Thread woke up after 1 second.
Thread will wait for notify...
Thread woken up after notify() or notifyAll() or timeout.

Frequently Asked Questions

Q. What is the difference between notify() and notifyAll()?

The notify() will only wake up a single thread that is waiting for a lock whereas notifyAll() will wake up all the waiting threads for a locked object.

Q. What is yield() used for?

The yield() is a way of pausing the execution of a thread so that other threads can execute the critical section. It is used to give other threads of the same or higher priority a chance to execute. If all the other threads have a lower priority, then the current thread will continue its execution.

Q. What is a synchronized block in Java?

A synchronized block is a block that can be accessed by only a single thread at a time. Synchronized blocks use monitors or locks to achieve synchronization of a particular object.

Conclusion

In this short tutorial, we learned about the differences between the wait() and the sleep() methods that are used with threads in Java. We simply need to remember that wait() is used for thread synchronization and does everything by keeping in mind the presence of other threads. Sleep, on the other hand, is mostly used for a single thread and for time synchronization.



About the author:
I am a 3rd-year Computer Science Engineering student at Vellore Institute of Technology. I like to play around with new technologies and love to code.