Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Java Thread.sleep: a pause, not synchronization

`Thread.sleep(long)` pauses the current thread for a requested number of milliseconds. It does not wait for another thread to finish or guarantee task order.

Version: Java SE 21

Syntax

public static void sleep(long millis) throws InterruptedException
public static void sleep(long millis, int nanos) throws InterruptedException
public static void sleep(java.time.Duration duration) throws InterruptedException

In Java SE 21, Thread.sleep is a static method of java.lang.Thread and returns no value. It pauses only the thread executing the call. Writing worker.sleep(50L) does not put worker to sleep: use the class name to make it clear that the current thread is the one being paused.

Thread.sleep(long) takes milliseconds; a negative value throws IllegalArgumentException. Thread.sleep(long, int) takes nonnegative milliseconds plus 0 to 999999 extra nanoseconds; an out-of-range argument throws the same exception. Thread.sleep(Duration), available since Java 19, takes a java.time.Duration and does nothing for a negative duration. The exact time at which execution resumes depends on system timers and scheduling; a nanosecond argument does not guarantee nanosecond precision.

Thread.sleep(long) can throw the checked exception InterruptedException if the thread is already interrupted on entry or is interrupted during the pause. Throwing the exception clears the interrupt status. Propagate the exception or, if you cannot, typically restore the status with Thread.currentThread().interrupt() and stop the current operation.

Sleeping does not release monitors the thread holds, including inside synchronized, and does not itself establish a happens-before relationship between threads. It therefore cannot make another thread's writes reliably visible. A Thread.sleep(0) call in one iteration does not demonstrate a measurable delay or parallel work. Adding Thread.sleep(5) to every task does not establish the order in which the tasks finish.

Use sleep to delay the current thread on purpose. To wait for another thread to finish, call Thread.join() on that thread; to obtain a task result, use Future.get(); to wait for one or more signals, use CountDownLatch.await().

Examples

public class SleepDemo {
    public static void main(String[] args) {
        System.out.println("Before sleep");
        try {
            Thread.sleep(50L);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            System.out.println("Interrupted");
            return;
        }
        System.out.println("After sleep");
    }
}

Result

Before sleep
After sleep

The same thread prints both expected lines. The example neither measures the pause nor runs work in parallel. If the pause is interrupted, the program restores the interrupt status, prints `Interrupted` instead of `After sleep`, and returns from `main`.

Related lessons

  • Java · Lesson 26 · Advanced · Version: 2
  • Java · Lesson 49 · Intermediate · Version: 2
  • Java · Lesson 50 · Intermediate · Version: 2

Sources