TimeUnit sleep() method in Java with Examples Last Updated : 15 Oct, 2018 Comments Improve Suggest changes Like Article Like Report 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 // 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 // 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 Comment More infoAdvertise with us Next Article TimeUnit sleep() method in Java with Examples R RishabhPrabhu Follow Improve Article Tags : Java Practice Tags : Java Similar Reads OffsetTime until() Method in Java with Examples until() method of the OffsetTime class used to calculate the amount of time between two OffsetTime objects using TemporalUnit. The start and end points are this and the specified OffsetTime passed as a parameter. The result will be negative if the end is before the start. The calculation returns a w 2 min read TimeUnit toHours() method in Java with Examples The toHours() method of TimeUnit Class is used to get the time represented by the TimeUnit object, as the number of Hours, since midnight UTC on the 1st January 1970. Syntax: public long toHours(long duration) Parameters: This method accepts a mandatory parameter duration which is the duration in mi 2 min read Timer purge() method in Java with Examples The purge() method of Timer class in Java is used to remove all the cancelled tasks from this queue of the Timer. The behaviour of the time remains unaffected by the calling of this method. Syntax: public int purge() Parameters: The method does not take any parameters. Return Value: The method retur 2 min read TimeUnit toNanos() method in Java with Examples The toNanos() method of TimeUnit Class is used to get the time represented by the TimeUnit object, as the number of NanoSeconds, since midnight UTC on the 1st January 1970.Syntax: public long toNanos(long duration) Parameters: This method accepts a mandatory parameter duration which is the duration 2 min read TimeUnit toMillis() method in Java with Examples The toMillis() method of TimeUnit Class is used to get the time represented by the TimeUnit object, as the number of Seconds, since midnight UTC on the 1st January 1970. Syntax: public long toMillis(long duration) Parameters: This method accepts a mandatory parameter duration which is the duration i 2 min read Like