Examples of Using Thread.sleep() in Java

When the system running the threads is occupied, the thread’s slumber period typically exceeds the time specified in the arguments. Conversely, if the system executing the sleep() function is less busy, the thread’s real sleep time is nearly equivalent to the specified time in the argument.

The
Java Thread
class offers two versions of
sleep() Method
: one that requires a single argument and another that takes two. To pause the execution of a thread for a specified duration,
method sleep
() is utilized, and the thread’s sleep duration is referred to as its
sleeping time
. Once the sleep time has elapsed, the thread resumes its execution from where it left off.

The sleep() Method Syntax:

Below are the
sleep() method
syntax guidelines.

public static void sleep(long mls) throws InterruptedException 
public static void sleep(long mls, int n) throws InterruptedException 

The sleep() method with one parameter is a native method whose implementation is done in another programming language. On the other hand, methods with two parameters are not native and are implemented in Java. The Thread class can be used to access the sleep() method since its signature includes the static keyword. Both native and non-native methods can throw exceptions, and either try-catch block or the throws keyword can be used to handle them.

The sleep() method, denoted by
Thread.sleep
, can be utilized by any thread, including
main thread
or any other thread.

Parameters:

These are the sleep() method’s utilized parameters.

The parameter mls denotes the time in milliseconds. The sleep() method determines the duration for which the thread will rest.

The programmer or developer can specify the duration for which the thread should remain in the sleeping state using the parameter “n”. The value of “n” can range from 0 to 999999.

The method does not return anything.

Important Points to Remember About the Sleep() Method

The execution of the current thread is always suspended whenever the Thread.sleep() method is called.

If a thread is sleeping and another thread interrupts it, an InterruptedException will be thrown.

The time a thread sleeps may vary depending on the system’s load. When the system is busy, the actual sleeping time of the thread is typically longer than the time passed in the argument. Conversely, when the system executing the sleep() method has less load, the actual sleeping time is approximately the same as the time specified in the argument.

Example of the sleep() method in Java : on the custom thread

The custom thread can be utilized with the sleep() method, as demonstrated in the following instance.

The name of the file is TestSleepMethod1.java.

class TestSleepMethod1 extends Thread{  
 public void run(){  
  for(int i=1;i<5;i++){ 
  // the thread will sleep for the 500 milli seconds 
    try{Thread.sleep(500);}catch(InterruptedException e){System.out.println(e);}  
    System.out.println(i);  
  }  
 }  
 public static void main(String args[]){  
  TestSleepMethod1 t1=new TestSleepMethod1();  
  TestSleepMethod1 t2=new TestSleepMethod1();  
   
  t1.start();  
  t2.start();  
 }  
}  


Output:

1
1
2
2
3
3
4
4

It is common knowledge that only one thread can be executed at a time. By putting a thread to sleep for a specific duration, it allows other threads, including
thread scheduler picks
, to run.

Example of the sleep() Method in Java : on the main thread

The name of the file is TestSleepMethod2.java.

// important import statements
import java.lang.Thread;
import java.io.*;

public class TestSleepMethod2
{
    // main method
public static void main(String argvs[])
{

try {
for (int j = 0; j < 5; j++)
{

// The main thread sleeps for the 1000 milliseconds, which is 1 sec
// whenever the loop runs
Thread.sleep(1000);

// displaying the value of the variable
System.out.println(j);
}
}
catch (Exception expn) 
{
// catching the exception
System.out.println(expn);
}
}
}


Output:

0
1
2
3
4
Example of the sleep() Method in Java: When the sleeping time is -ive

When the time for sleeping is negative, an IllegalArguementException is thrown by the example below.

The name of the file is TestSleepMethod3.java.

// important import statements
import java.lang.Thread;
import java.io.*;

public class TestSleepMethod3
{
// main method
public static void main(String argvs[])
{
// we can also use throws keyword followed by
// exception name for throwing the exception
try 
{
for (int j = 0; j < 5; j++) 
{

// it throws the exception IllegalArgumentException
// as the time is -ive which is -100
Thread.sleep(-100);

// displaying the variable's value
System.out.println(j);
}
}
catch (Exception expn) 
{

// the exception iscaught here 
System.out.println(expn);
}
}
}


Output:

java.lang.IllegalArgumentException: timeout value is negative

Frequently Asked Questions