Categories: Java

Thread.sleep() method in Java

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

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago