Table of contents
1.
Introduction
2.
What is Java Thread.sleep()?
3.
Syntax of Sleep() Method in Java
4.
Parameters In Thread.Sleep() Method
5.
Example of Thread.Sleep() Method in Java
5.1.
Example 1
5.2.
Java
5.3.
Example 2
5.4.
Java
6.
Important Points to Remember about the Java Thread.Sleep() Method
7.
Frequently Asked Questions
7.1.
What does sleep method do in Java?
7.2.
What are the wait () and sleep () methods?
7.3.
Why is the sleep method static in Java?
7.4.
What is yield () and sleep () in thread Java?
7.5.
Does sleep () belongs to thread class?
8.
Conclusion
Last Updated: Oct 25, 2024
Easy

Thread.sleep() in Java

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The Thread.sleep() method plays a fundamental role in thread management by allowing threads to pause their execution for a specified duration, it allows other threads to execute, improving resource management and preventing CPU overload. This method also helps simulate delays in applications, enhancing concurrency

Thread.sleep() in Java

What is Java Thread.sleep()?

A thread class is a thread execution of a program. They are present in Java.lang package. This class consists of the Sleep() method, composed of two overloaded Sleep methods (). They are one with one argument and another with a different argument. The main function of the sleep() method is to halt the code execution for the current Thread for a specific amount of time. After the duration gets over, the Thread executing will continue to run again.

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

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

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

  1. Whenever the Thread.sleep() method is called, it always halts the current Thread's execution.
  2. The InterruptedException is called when the current Thread is disturbed by another thread.
  3. 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!”.

Live masterclass