Thread in Java is implemented where parallelism is required. In Java, the thread class is used for parallel execution of tasks. This thread class consists of functions, methods, and constructors that can be used while coding to accomplish different tasks. One of the frequently utilized methods of the Thread class is the Thread.sleep() method.
This article will elaborate on the working of the Thread.sleep() method in Java.
How to Use the Thread.sleep() Method in Java?
The Thread.sleep() method in Java halts the execution process since it makes the current thread sleep for a specific time during the execution of code. After the time ends, the thread wakes up and executes as normal. The sleep time in Thread.sleep() can be used in two different ways.
The default method takes the time in milliseconds and nanoseconds as shown in two different syntaxes below
public static void sleep(long mls) throws InterruptedException
In the above syntax
- The sleep method takes time in milliseconds.
- Throws an interrupted exception.
Another way of representing the Thread.sleep() method is
public static void sleep(long mls, int n) throws InterruptedException
In the above syntax, the “int n” is an additional parameter that takes the time in nanoseconds in the range of 0-999999.
Example 1: Simple Implementation of Thread.sleep() Method in Java
The code below shows the working of the Thread.sleep() in Java.
class ThreadExample1{
//main() method
public static void main(String[] args) throws InterruptedException {
long s = System.currentTimeMillis();
System.out.println("Thread.sleep() in Java: ");
//Make the thread sleep for 1500 milliseconds
Thread.sleep(1500);
//Print the time in ms when the thread wakes.
System.out.println("The Sleep time in milliseconds is : " + (System.currentTimeMillis() - s));
}
}
In the above code:
- The class is declared as “ThreadExample1” in Java.
- In the main() method of Java, the long data type is used to print the time in milliseconds.
- The thread is made to sleep for 1500 ms using the Thread.sleep() method.
- The output when the thread wakes up is printed on the screen.
Output
The thread sleeps for 1500 ms and performs the normal execution. It is crucial to note that the output may differ each time the code is executed.

Example 2: Basic Implementation Using Milliseconds and Nanoseconds
The code below shows that the Thread.sleep() method uses milliseconds and nanoseconds to make the thread sleep.
class ThreadExample2{
//main() method
public static void main(String[] args) throws InterruptedException {
long s = System.currentTimeMillis();
System.out.println("Thread.sleep() in Java: ");
//Make the thread sleep for 2500 milliseconds and 9897 milliseconds
Thread.sleep(2500, 9897);
//Print the time in ms when the thread wakes.
System.out.println("The Sleep time in milliseconds is : " + (System.currentTimeMillis() - s));
}
}
In the above code block, the thread sleeps for 2500ms and 9897ns.
Output
The output below shows the implementation of the Thread.sleep() method using nanoseconds.

Example 3: Thread.sleep() Method on the Main Thread in Java
The thread that instantly starts running is termed the “Main Thread”. The code below implements the Thread.sleep() method on the main thread in Java:
// Import the required package
import java.lang.Thread;
import java.io.*;
public class ThreadExample3{
// main method of Java
public static void main(String argvs[])
{
System.out.println("The Output for Thread.sleep() Method for Main Thread: ");
//try-catch block of Java
try {
for (int x = 0; x < 8; x++)
{
// The thread sleeps for 2500 milliseconds
Thread.sleep(2500);
System.out.println(x);
}
}
catch (Exception e)
{ System.out.println(e);
}
}
}
In the above Java code:
- Different packages are imported.
- “ThreadExample3” is declared as the class in the next step.
- In the try block, the for loop prints the condition seven times.
- The Thread.sleep() method makes the thread sleep for 2500 milliseconds.
- The catch block in Java is utilized to check if there are exceptions.
Output
The output values from 1 to 7 are printed with a lapse of 2500 milliseconds as depicted below.

Example 4: Thread.sleep() Method on the Custom Thread in Java
Custom threads are created using the run() method of the runnable interface. An object for the thread is created and the thread starts working using the start() method of the Thread class.
// Import the required package
import java.lang.Thread;
import java.io.*;
public class ThreadExample3 extends Thread {
public void run()
{
System.out.println("The Output for Thread.sleep() Method for Custom Thread: ");
//try-catch block of Java
try {
for (int x = 0; x < 8; x++)
{
// The thread sleeps for 2000 milliseconds
Thread.sleep(2000);
System.out.println(x);
}
}
catch (Exception e)
{
// catching the exception
System.out.println(e);
}
}
public static void main(String[] args)
{
// main thread
ThreadExample2 thr = new ThreadExample2();
thr.start();
}
}
In the above code block:
- The Thread from the Thread class is implemented using the “extend” keyword.
- The Runnable Interface utilizes the run() method to start the Thread.
- The next step involves the “for loop” which runs from 0 to 7.
- The thread.sleep() method makes the thread sleep for 2000 milliseconds.
- The catch block is utilized for any exceptions that may arise.
- The last part of the code involves the main() method of Java that runs the custom thread using the start() method of the Thread class.
Output
The output below shows that the integer values from 1 to 7 are printed with a time-lapse of 2000 ms.

Illegal Exception for Negative Time
The Thread.sleep() method throws an Exception when time is declared as a negative entity as shown in the code below.
class ThreadException{
//main() method
public static void main(String[] args) throws InterruptedException {
long s = System.currentTimeMillis();
System.out.println("Thread.sleep() in Java: ");
//Make the thread sleep for 1500 milliseconds
Thread.sleep(-2500);
//Print the time in ms when the thread wakes.
System.out.println("The Sleep time in milliseconds is : " + (System.currentTimeMillis() - s));
}
}
In the above piece of code, when the time is declared as a “-2500” in the Thread.sleep() method, an exception arises in Java and the output is not printed.
Output
The below output shows that the exception is printed as “ IllegalArgumentException: timeout value is negative ” which means that the time can not be negative.

This concludes the working of the Thread.sleep() method in Java.
Conclusion
The Thread.sleep() method in Java accepts time in milliseconds and stops the current thread from executing the task for a specified time period. It resumes after the specified duration. Apart from time in milliseconds an additional time to stop the execution of the current thread can be declared in nanoseconds. This article has demonstrated different methods to understand the working of Thread.sleep() in Java.