Saturday, October 4, 2025
HomeLanguagesJavaTimeUnit sleep() method in Java with Examples

TimeUnit sleep() method in Java with Examples

The sleep() method of TimeUnit Class is used to performs a Thread.sleep using this time unit. This is a convenience method that sleeps time arguments into the form required by the Thread.sleep method.

Syntax:

public void sleep(long timeout)
           throws InterruptedException

Parameters: This method accepts a mandatory parameters timeout which is the minimum time to sleep. If this is less than or equal to zero, then do not sleep at all.

Return Value: This method does not return anything.

Exception: This method throws InterruptedException if interrupted while sleeping.

Below program illustrate the implementation of TimeUnit sleep() method:

Program 1:




// Java program to demonstrate
// sleep() method of TimeUnit Class
  
import java.util.concurrent.*;
  
class GFG {
    public static void main(String args[])
    {
        // Get time to sleep
        long timeToSleep = 0L;
  
        // Create a TimeUnit object
        TimeUnit time = TimeUnit.SECONDS;
  
        try {
  
            System.out.println("Going to sleep for "
                               + timeToSleep
                               + " seconds");
  
            // using sleep() method
            time.sleep(timeToSleep);
  
            System.out.println("Slept for "
                               + timeToSleep
                               + " seconds");
        }
  
        catch (InterruptedException e) {
            System.out.println("Interrupted "
                               + "while Sleeping");
        }
    }
}


Output:

Going to sleep for 0 seconds
Slept for 0 seconds

Program 2:




// Java program to demonstrate
// sleep() method of TimeUnit Class
  
import java.util.concurrent.*;
  
class GFG {
    public static void main(String args[])
    {
        // Get time to sleep
        long timeToSleep = 10L;
  
        // Create a TimeUnit object
        TimeUnit time = TimeUnit.SECONDS;
  
        try {
  
            System.out.println("Going to sleep for "
                               + timeToSleep
                               + " seconds");
  
            // using sleep() method
            time.sleep(timeToSleep);
  
            System.out.println("Slept for "
                               + timeToSleep
                               + " seconds");
        }
  
        catch (InterruptedException e) {
            System.out.println("Interrupted "
                               + "while Sleeping");
        }
    }
}


Output:

Going to sleep for 10 seconds
Slept for 10 seconds
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32332 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6704 POSTS0 COMMENTS
Nicole Veronica
11868 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11929 POSTS0 COMMENTS
Shaida Kate Naidoo
6819 POSTS0 COMMENTS
Ted Musemwa
7080 POSTS0 COMMENTS
Thapelo Manthata
6775 POSTS0 COMMENTS
Umr Jansen
6776 POSTS0 COMMENTS