Java

Thread.sleep() Method in Java

In Java programming, there can be instances where the developer needs to halt the functionalities for a particular time span. For instance, appending features or effects in an application or ensuring the streamlined code flow. In such situations, the “Thread.sleep()” method in Java is of great aid in debugging the faced limitations by placing intervals in the code.

This write-up will elaborate on the usage and implementation of the “Thread.sleep()” method in Java.

What is the “Thread.sleep()” Method in Java?

The “sleep()” method of the “Thread” class is a static method utilized to stop the working of the current thread for a specific time period(in milliseconds). However, it(thread) resumes once the sleep time is over. Moreover, there is an associated exception “InterruptedException” that is thrown in the case of another thread interrupting the current thread while it is in sleep mode.

Syntax

public static void sleep(td, int add)

 
In this syntax:

    • td” refers to the time duration(in milliseconds) for which the thread must be asleep.
    • int add” corresponds to the additional time till which the thread needs to be in sleeping mode.

Before proceeding to the examples, make sure to include the provided library to work with “Thread”:

import java.lang.Thread;

 
Example 1: Use of “Thread.sleep()” Method in Java

In this example, the “Thread.sleep()” method can be utilized to cope with the current thread for the specified time and resume further functionalities after that. Also, the faced exception type during the execution can be specified using the “throws” keyword:

public class threadsleep {
 public static void main(String[] args) throws InterruptedException{
  System.out.println("Sleeping for 5 seconds!");
  Thread.sleep(5000);
  System.out.println("Success!");
}}

 
In the above code snippet:

    • Firstly, handle the probable exception, i.e., “InterruptedException” in the code via the “throws” keyword.
    • After that, apply the “Thread.sleep()” method having the sleep time(in milliseconds) as its parameter.
    • The code will execute such that the thread will sleep for “5 seconds” after displaying the former message and the latter message will be displayed after the elapsed sleep time.

Note: In this case, the specified exception cannot be thrown since the thread is not interrupted.

Output


 

In this output, it can be seen that the thread sleeps and awakes accordingly.

Example 2: Use of “Thread.sleep()” Method and Handling/Printing the Exception via “try…catch” Statement in Java

The “try…catch” statement is used to execute a particular code and handle the exception(s) faced while executing it, respectively. The “start()” method initiates the execution of the thread and the “interrupt()” method interrupts the thread. The “printStackTrace()” method, however, logs all the details associated with the faced exception like line number, class, etc.

These approaches can be applied in combination to start and interrupt the thread and log the detailed faced exception while doing so:

public class threadsleep2 {
 static class threadSleep extends Thread {
  public void run() {
   try {
    Thread.sleep(2000);
    System.out.println("Sleeping for 2 seconds!");
}
    catch (InterruptedException e) {
     e.printStackTrace();
}
}}
 public static void main(String[] args) {
  threadSleep obj = new threadSleep();
  obj.start();
  obj.interrupt();
}}

 
In this code block, perform the following steps:

    • First, create a thread via the “extends” keyword followed by the class “threadSleep”.
    • In the class definition, override the “run()” method to start a new thread.
    • Now, contain the “Thread.sleep()” method in the “try” block having the specified sleep time.
    • In the “catch” block, cope with the probable exception and display it in detail using the “printStackTrace()” method.
    • In the “main()” method, create an object of the class named “obj” using the “new” keyword and the “threadSleep()” constructor, respectively.
    • In the next step, associate the “start()” and “interrupt()” methods with the created object to start the “run()” method’s execution and interrupt the created thread, respectively.
    • This will resultantly log the “InterruptedException” limitation, as discussed.

Output


In this outcome, it can be implied that the specified exception is encountered and logged with complete details since the “Thread” becomes interrupted on the console.

Conclusion

The Java “Thread.sleep()” is a static method that is utilized to stop the working/execution of the current thread for a specific time period(in milliseconds). There is an associated exception named “InterruptedException” that is faced when another thread interrupts/interferes with the current thread when it is in sleep mode. This blog discussed the usage and implementation of the “Thread.sleep()” method in Java.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.