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

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

3 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

3 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

3 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

4 months ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

4 months ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

4 months ago