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.
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
It does not return any value, i.e., the return type of the sleep function is void.
// 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);
}
}
}
0
1
2
3
4
5
// 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();
}
}
0
1
2
3
4
5
// 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);
}
}
}
java.lang.IllegalArgumentException: timeout value is negative
Note: also read about the Java join() method
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.
You are given two singly linked lists that intersect at some node. Your task is…
A builder plans to construct N houses in a row, where each house can be…
Find the length of the longest absolute path to a file within the abstracted file…
You manage an e-commerce website and need to keep track of the last N order…
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…