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.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…