Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Thread.sleep()?
2.1.
Syntax of Sleep() Method in Java
2.2.
Parameters In Thread.Sleep() Method
3.
Example of Thread.Sleep() Method in Java
3.1.
Example 1
3.2.
Java
3.3.
Example 2
3.4.
Java
4.
Important Points to Remember about the Sleep() Method
5.
Frequently Asked Questions
5.1.
What does sleep method do in Java?
5.2.
What are the wait () and sleep () methods?
5.3.
How to sleep for 1 sec in Java?
5.4.
Why is the sleep method static in Java?
6.
Conclusion
Last Updated: May 15, 2024
Easy

Thread.sleep() in Java

Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

Welcome to our blog on Thread.sleep() in Java. In multi-threaded Java applications, precise control over thread execution is crucial for achieving desired behaviors, managing resources, and synchronizing tasks. The Thread.sleep() method plays a fundamental role in thread management by allowing threads to pause their execution for a specified duration. 

Thread.sleep() in Java

What is 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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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);
}
}
}

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();   
}   
}   

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 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.

How to sleep for 1 sec in Java?

To sleep for 1 second in Java, you can call the sleep() method with a parameter specifying the duration in milliseconds. For example, Thread.sleep(1000); will pause the current thread's execution for 1 second.

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.

Conclusion

In this article, we have discussed the topic of Java's sleep() method. We briefly introduced sleep() methods and their syntax and working. We also gave a few points that might need to be considered before implementing the sleep() method. If you want to know how a thread works, visit 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