Thread.sleep() in Java
The Thread.sleep() method in Java is used to pause the current thread for a given amount of time. While a thread sleeps, it is in the TIMED_WAITING state and uses no CPU; other threads keep running. When the time is up, the thread becomes runnable again. sleep is a static method, so it always pauses the thread that calls it. It throws the checked exception InterruptedException if another thread interrupts the sleeper. In this lesson, you will learn how to use sleep correctly.
Declaration
public static void sleep(long millis) throws InterruptedException
public static void sleep(long millis, int nanos) throws InterruptedException
public static void sleep(Duration duration) throws InterruptedExceptionThe Duration version was added in Java 19.
Example: A Simple Countdown
Let's see an example that counts down once per 200 milliseconds and measures the total time.
import java.time.Duration;
public class Countdown {
public static void main(String[] args) throws InterruptedException {
long start = System.nanoTime();
for (int i = 3; i >= 1; i--) {
System.out.println(i + "...");
Thread.sleep(Duration.ofMillis(200));
}
System.out.println("Go, Navika!");
long ms = (System.nanoTime() - start) / 1_000_000;
System.out.println("At least 600 ms passed? " + (ms >= 600));
}
}3...
2...
1...
Go, Navika!
At least 600 ms passed? trueExplanation: Each sleep pauses the main thread for at least 200 milliseconds, so three of them take at least 600. Sleeping may take slightly longer than requested, because the thread must wait for the scheduler to give it a CPU again, but it never takes less (unless interrupted).
Example: Handling InterruptedException
public class InterruptSleep {
public static void main(String[] args) throws InterruptedException {
Thread sleeper = new Thread(() -> {
try {
System.out.println("Srihitha sleeps for 5 seconds");
Thread.sleep(5000);
System.out.println("Woke up normally");
} catch (InterruptedException e) {
System.out.println("Woken early by an interrupt");
Thread.currentThread().interrupt();
}
});
sleeper.start();
Thread.sleep(200);
sleeper.interrupt();
sleeper.join();
}
}Srihitha sleeps for 5 seconds
Woken early by an interruptExplanation: The main thread interrupts the sleeper after 200 milliseconds, so sleep ends early with InterruptedException. The catch block restores the thread's interrupted flag with interrupt(), the recommended practice, so code further up can still see that the thread was asked to stop. Interruption is covered fully in "Interrupting a Thread in Java".
Common Mistakes
Swallowing InterruptedException with an empty catch. The stop request is lost; restore the flag or stop.
Calling t.sleep(...) on another thread. sleep is static and always pauses the current thread.
Using sleep to wait for another thread to finish. Use join.
Practice
- Easy: Print the time in seconds every half second, five times, using
sleepandSystem.currentTimeMillis(). - Harder: Write a retry loop that tries an operation up to 3 times, sleeping 100, 200 and 400 milliseconds between attempts.
Key Takeaways
Was this tutorial helpful?