Java Thread.sleep() Method

Last Updated : 15 Jan 2026

The Java Thread class provides the two variants of the sleep() method. First one accepts only an argument, whereas the other variant accepts two arguments. The method sleep() is being used to halt the working of a thread for a given amount of time. The time up to which the thread remains in the sleeping state is known as the sleeping time of the thread. After the sleeping time is over, the thread starts its execution from where it has left.

What is Thread.sleep()?

The Thread.sleep() method is used to pause the execution of the current thread for a specified amount of time. During this period, the thread does not consume CPU resources and moves to the timed waiting state. After the specified time elapses, the thread becomes runnable again and waits for the scheduler to pick it for execution.

To understand the various states and life cycle of a thread, read: Thread Life Cycle.

Syntax

The syntaxes with different parameters of the Thread.sleep() are as follow:

Here,

  • The version with one parameter (millis) is a native method; the version with two parameters (millis, nanos) is implemented in Java.
  • Since it throws a checked exception, you must use a try-catch block or declare throws InterruptedException.
  • Any thread, including the main thread, can call Thread.sleep().

Parameters of Thread.sleep()

The sleep() method takes the following parameters:

  • millis (mls): Specifies the time in milliseconds for which the thread will sleep.
  • nanos (n): Specifies additional nanoseconds (0 to 999,999) to pause the thread for more precise timing.

Return Type

The sleep() method does not return any value.

Examples of Thread.sleep() Method

The following are examples showing different use cases of the Thread.sleep() method.

Example 1: Simple Sleep on Main Thread

The following example demonstrates a simple pause of 2 seconds in the main thread using Thread.sleep().

Output:

Start
End

In this example, the main thread prints "Start", pauses for 2 seconds, and then prints "End".

Example 2: Handling InterruptedException

The following example shows how to handle InterruptedException when a sleeping thread is interrupted by another thread.

Output:

Thread will sleep for 5 seconds.
Thread was interrupted during sleep.

In this code, the child thread sleeps for 5 seconds but gets interrupted by the main thread after 2 seconds. The catch block handles the interruption gracefully.

Example 3: Sleep in a Custom Thread

The following example demonstrates using Thread.sleep() in a custom thread created by extending the Thread class.

Output:

1
1
2
2
3
3
4
4

As you know well that at a time only one thread is executed. If you sleep a thread for the specified time, the thread scheduler picks up another thread and so on.

Example 4: Sleep in Main Thread with Loop

The following example shows how the main thread can sleep in a loop.

Output:

0
1
2
3
4

Example 5: Sleep with Negative Time

The following example demonstrates that Thread.sleep() throws an exception when the sleeping time is negative.

Output:

java.lang.IllegalArgumentException: timeout value is negative