VOOZH about

URL: https://www.geeksforgeeks.org/java/difference-between-wait-and-sleep-in-java/

⇱ Difference between wait and sleep in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Difference between wait and sleep in Java

Last Updated : 21 Jan, 2026

In Java, wait() and sleep() are commonly used methods that pause the execution of a thread. Although both seem similar, they differ significantly in behavior, usage, and purpose, especially in multithreaded environments.

sleep() Method

The sleep() method is a static method of the Thread class. It temporarily pauses the execution of the currently executing thread for a specified period of time.

  • Causes the thread to pause for a fixed time
  • Does not release the lock held by the thread
  • Can be used outside synchronized blocks
  • Throws InterruptedException

Syntax

public static void sleep(long millis) throws InterruptedException

Example:

Output:

Thread going to sleep
Thread woke up

Explanation: The current thread pauses execution for 2 seconds and then resumes automatically after the specified time.

wait() Method

The wait() method is a non-static method of the Object class. It causes the current thread to wait until another thread invokes notify() or notifyAll() on the same object.

  • Belongs to java.lang.Object
  • Must be called inside a synchronized block
  • Releases the lock held on the object
  • Thread resumes only after notification
  • Throws InterruptedException

Syntax

public final void wait() throws InterruptedException

Example:


Output
Thread waiting
Thread notifying
Thread resumed

Explanation: The first thread waits until the second thread calls notify() on the same object. During waiting, the lock is released and later reacquired.

👁 Difference-between-wait-and-sleep-in-Java

The diagram illustrates how a thread moves between different states when wait() and sleep() methods are used.

wait() vs sleep()

Wait()Sleep()
Wait() method belongs to Object class.Sleep() method belongs to Thread class.
Wait() method releases lock during Synchronization.Sleep() method does not release the lock on object during Synchronization.
Wait() should be called only from Synchronized context.There is no need to call sleep() from Synchronized context.
Wait() is not a static method. Sleep() is a static method. 

Wait() Has Three Overloaded Methods:

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

Sleep() Has Two Overloaded Methods:

  • sleep(long millis)millis: milliseconds
  • sleep(long millis,int nanos) nanos: Nanoseconds
public final void wait(long timeout)public static void sleep(long millis) throws Interrupted_Execption
Comment