Syntax of Sleep() Method in Java
public static void sleep(long mls)throws InteruptedExecution
public static void sleep(long mls, int n)throws InterruptedException
Parameters In Thread.Sleep() Method
millis (mls): The time in milliseconds, represented by the parameter mls. The duration for which the thread will sleep is governed by this sleep method ().
nanos (n): This shows the range to which the additional time up to which the programmer wants the Thread to be is sleep mode.
Example of Thread.Sleep() Method in Java
Example 1
Java
import java.lang.Thread;
public class ThreadSleep {
public static void main(String[] args) {
try {
for (int i = 0; i < 5; i++) {
Thread.sleep(2000);
System.out.println(i);
}
}
catch (Exception e) {
System.out.println(e);
}
}
}

You can also try this code with Online Java Compiler
Run Code
Output
0
1
2
3
4
In this example, the code uses the sleep to print a number after waiting for some time.
Example 2
Java
import java.lang.Thread;
public class ThreadsSleeps {
public void run(){
for(int i=1;i<5;i++){
try{
Thread.sleep(500);
}
catch(InterruptedException e)
{
System.out.println(e);
}
System.out.println(i);
}
}
public static void main(String args[]){
ThreadsSleeps t1=new ThreadsSleeps();
ThreadsSleeps t2=new ThreadsSleeps();
t1.run();
t2.run();
}
}

You can also try this code with Online Java Compiler
Run Code
Output
1
2
3
4
1
2
3
4
We have used the try-catch exception in the thread sleep method in this example. The Thread will sleep for some time and will print the output.
Important Points to Remember about the Java Thread.Sleep() Method
- Whenever the Thread.sleep() method is called, it always halts the current Thread's execution.
- The InterruptedException is called when the current Thread is disturbed by another thread.
- The sleeping time of the Thread will increase if the system is busy. The system will have more load than a normal system with less load.
Frequently Asked Questions
What does sleep method do in Java?
The sleep() method in Java is used to pause the execution of the current thread for a specified duration of time. It allows threads to temporarily relinquish CPU time, facilitating controlled concurrency and timing in multi-threaded applications.
What are the wait () and sleep () methods?
Both wait() and sleep() methods are used for thread suspension, but they serve different purposes. wait() is used for inter-thread communication and synchronization, while sleep() is primarily used for introducing delays in thread execution.
Why is the sleep method static in Java?
The sleep() method is static in Java because it is invoked directly on the Thread class to pause the execution of the current thread. Being static allows the method to be called without the need for an instance of the Thread class, facilitating simpler and more concise code.
What is yield () and sleep () in thread Java?
In Java, yield() temporarily pauses the currently executing thread to allow other threads of equal priority to run. sleep(), on the other hand, pauses a thread for a specified duration, enabling other threads to execute while conserving CPU resources.
Does sleep () belongs to thread class?
Yes, sleep() is a static method in Java’s Thread class. It pauses the current thread’s execution for a given time, specified in milliseconds, facilitating better resource management and enabling controlled delays in multi-threaded applications.
Conclusion
This article dicussed Java's sleep() method, including an introduction to its syntax and functionality. We’ve also highlighted key considerations for implementing the sleep() method effectively. To learn more about thread operations, check out our guide on the Thread Scheduler. I hope that this blog might have increased your knowledge. Please upvote my article and other ninja's articles if you like this article. Your support gives us the strength to write more amazing content like these. “Happy coding!”.