Thread.sleep() in Java with Examples
The Java Thread class provides the two variant of the sleep() method. First one accepts only an arguments, whereas the other variant accepts two arguments. The method sleep() is being used to halt the working of a thread for a given amount of time. The time up to which the thread remains in the sleeping state is known as the sleeping time of the thread. After the sleeping time is over, the thread starts its execution from where it has left.
The sleep() Method Syntax:
Following are the syntax of the sleep() method.
The method sleep() with the one parameter is the native method, and the implementation of the native method is accomplished in another programming language. The other methods having the two parameters are not the native method. That is, its implementation is accomplished in Java. We can access the sleep() methods with the help of the Thread class, as the signature of the sleep() methods contain the static keyword. The native, as well as the non-native method, throw a checked Exception. Therefore, either try-catch block or the throws keyword can work here.
The Thread.sleep() method can be used with any thread. It means any other thread or the main thread can invoke the sleep() method.
Parameters:
The following are the parameters used in the sleep() method.
mls: The time in milliseconds is represented by the parameter mls. The duration for which the thread will sleep is given by the method sleep().
n: It shows the additional time up to which the programmer or developer wants the thread to be in the sleeping state. The range of n is from 0 to 999999.
The method does not return anything.
Important Points to Remember About the Sleep() Method
Whenever the Thread.sleep() methods execute, it always halts the execution of the current thread.
Whenever another thread does interruption while the current thread is already in the sleep mode, then the InterruptedException is thrown.
If the system that is executing the threads is busy, then the actual sleeping time of the thread is generally more as compared to the time passed in arguments. However, if the system executing the sleep() method has less load, then the actual sleeping time of the thread is almost equal to the time passed in the argument.
Example of the sleep() method in Java : on the custom thread
The following example shows how one can use the sleep() method on the custom thread.
FileName: TestSleepMethod1.java
Output:
1 1 2 2 3 3 4 4
As you know well that at a time only one thread is executed. If you sleep a thread for the specified time, the thread scheduler picks up another thread and so on.
Example of the sleep() Method in Java : on the main thread
FileName: TestSleepMethod2.java
Output:
0 1 2 3 4
Example of the sleep() Method in Java: When the sleeping time is -ive
The following example throws the exception IllegalArguementException when the time for sleeping is negative.
FileName: TestSleepMethod3.java
Output:
java.lang.IllegalArgumentException: timeout value is negative