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.
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:
Going to sleep for 0 seconds Slept for 0 seconds
Going to sleep for 10 seconds Slept for 10 seconds
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.