Thread.sleep() method in Java:
The two variations of the sleep() method are available from the Java Thread class. The first one only takes one argument, whereas the other variant takes two. To stop a thread from working for a specified period of time, use the method sleep(). The length of time the thread spends sleeping is referred to as the thread’s sleeping time. The thread resumes execution, where it left off after the sleeping period, is over.
Syntax of Sleep() Method:
The sleep() method in Java Thread has 4 variants. Which are:
public static void sleep(long millis)throws InterruptedException
public static void sleep(long millis)throws IllegalArguementException
public static void sleep(long millis, int nanos)throws InterruptedException
public static void sleep(long millis, int nanos)throws IllegalArguementException
Important Information About the Thread.sleep() Method:
- Method The execution of the current thread is always paused whenever Thread.sleep() functions.
- When a thread is sleeping and another thread interrupts, an InterruptedException is thrown.
- The actual amount of time the thread will sleep depends on how busy the system is. If the system is under less load, the actual amount of time the thread will sleep is similar to the amount of time that was passed when calling the sleep() method.
Parameters Passed In Thread.Sleep() Method:
- millis: The number of milliseconds a thread will sleep.
- nanos: The additional amount of time in nanoseconds that we want the thread to sleep is indicated here. It is 0 to 999999 wide.
Return Type of Sleep() Method:
It does not return any value, i.e., the return type of the sleep function is void.
Example -Using Thread.Sleep() Method For Main Thread:
// Java Program for sleeping the main thread.
import java.io.*;
import java.lang.Thread;
class Coderz {
public static void main(String[] args)
{
// we can also use throws keyword followed by
// exception name for throwing the exception
try {
for (int i = 0; i < 6; i++) {
// it will sleep the main thread for 2 sec
// ,each time the for loop runs
Thread.sleep(2000);
// printing the value of the variable
System.out.println(i);
}
}
catch (Exception e) {
// catching the exception
System.out.println(e);
}
}
}
Output:
0
1
2
3
4
5
Example -Using Thread.Sleep() Method For Custom Thread
// Java Program for sleeping the custom thread.
import java.io.*;
import java.lang.Thread;
class Coderz extends Thread {
public void run()
{
// thread 0
// we can also use throws keyword followed by
// exception name for throwing the exception
try {
for (int i = 0; i < 6; i++) {
// it will sleep the main thread for 2 sec
// ,each time the for loop runs
Thread.sleep(2000);
// This Thread.sleep() method will sleep the
// thread 0.
// printing the value of the variable
System.out.println(i);
}
}
catch (Exception e) {
// catching the exception
System.out.println(e);
}
}
public static void main(String[] args)
{
// main thread
Coderz obj = new Coderz();
obj.start();
}
}
Output:
0
1
2
3
4
5
Example -IllegalArguementException When Sleep Time Is Negative:
// Java Program for showing how exception can occur if we
// pass the negative timeout value.
import java.io.*;
import java.lang.Thread;
class Coderz {
public static void main(String[] args)
{
// we can also use throws keyword followed by
// exception name for throwing the exception
try {
for (int i = 0; i < 6; i++) {
// this will throw the
// IllegalArgumentException
Thread.sleep(-100);
// printing the value of the variable
System.out.println(i);
}
}
catch (Exception e) {
// catching the exception
System.out.println(e);
}
}
}
Output:
java.lang.IllegalArgumentException: timeout value is negative
Note: also read about the Java join() method
Follow Me
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Leave a Comment