Java Thread.sleep() MethodLast 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. SyntaxThe syntaxes with different parameters of the Thread.sleep() are as follow: Here,
Parameters of Thread.sleep()The sleep() method takes the following parameters:
Return TypeThe sleep() method does not return any value. Examples of Thread.sleep() MethodThe following are examples showing different use cases of the Thread.sleep() method. Example 1: Simple Sleep on Main ThreadThe 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 InterruptedExceptionThe 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 ThreadThe 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 LoopThe following example shows how the main thread can sleep in a loop. Output: 0 1 2 3 4 Example 5: Sleep with Negative TimeThe following example demonstrates that Thread.sleep() throws an exception when the sleeping time is negative. Output: java.lang.IllegalArgumentException: timeout value is negative Next TopicCan we start a Thread Twice |
We request you to subscribe our newsletter for upcoming updates.