Java Thread sleep() method

In this tutorial, we will learn about Java Thread sleep method and how it helps to pause a thread from its execution until certain point of time and examples.

Thread sleep() method

Sleep is method from java.lang.Thread class and helps to pause a thread execution until certain of point of time. Sleeping or pausing an execution of a thread helps to reduce the consumption of CPU cycles. Sleep method is declared as a native method of Thread class.

sleep() method arguments

java.lang.Thread class contains two overloaded sleep methods, as described following.

  1. sleep(long millis)
  2. sleep(long millis, int nanos)

sleep(long millis)

This method takes one argument of long datatype, unit of argument in the form of duration is milliseconds.

If we want to sleep a thread for duration of 10seconds, then the value in milliseconds will be 10*1000 = 10000milliseconds.

sleep(long millis, int nanos)

This method takes two arguments one is of long datatype and another one is with int datatype, unit of argument one is with milliseconds and another one with nanoseconds.

Second argument nanos will be additional nanoseconds to sleep the thread from its execution.

Both the sleep method throws the Interrupted Exception, in case if we want to use sleep method either throw InterruptedException or catch the InterruptedException.

Thread sleep() method example:

CopiedCopy Code
import java.util.Date;
public class SleepOfThread extends Thread {
	public static void main(String[] args) {
		Thread thread=new SleepOfThread ();
		thread.start();
	}
	public void run()
	{
		System.out.println("Thread going for a sleep on : "+new Date());
		try {
			sleep(10000L);//sleeping for 10seconds
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("Thread woken after a sleep of 10s at : "+new Date());
	}
}

Output:

Thread going for a sleep on : Wed Apr 12 17:21:16 IST 2023
Thread woken after a sleep of 10s at : Wed Apr 12 17:21:26 IST 2023

The above output gets generated after executing the example, first will be printed after starting a thread and goes for a sleep of 10s and next line gets printed after 10s.

Example2 of Thread sleep method

CopiedCopy Code
import java.util.Date;
public class SleepOfThread extends Thread {
	public static void main(String[] args) {
		Thread thread=new SleepOfThread ();
		thread.start();
	}
	public void run()
	{
		System.out.println("Thread going for a sleep on : "+new Date());
		try {
			sleep(10000L,5000);//sleeping for 10seconds and 5000 nanoseconds
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("Thread woken after a sleep of 10s at : "+new Date());
	}
}

Output :

Thread going for a sleep on : Wed Apr 12 17:24:01 IST 2023
Thread woken after a sleep of 10s at : Wed Apr 12 17:24:11 IST 2023

As we have passed nanoseconds just 5000 do not have much of a time difference in the second line time as compared to example1.

Use cases of sleep method

  1. Pausing an execution of a current thread for a definite period.
  2. Allows to wait for other threads to complete their execution or completion of its tasks.
  3. Gives the CPU cycles voluntarily which in terms helps reduce the usage CPU.
  4. Helps to execute a Task after a desired period.
  5. Can be helpful to monitor the other processes or Threads after a period endlessly.

Benefits of sleep method

  1. Easy to implement.
  2. Usage of CPU cycles can be reduced.
  3. Suitable for monitoring purpose of an application or threads itself.

NOTE: Before choosing the sleep method for monitoring purpose, consider examining the java.util.Timer and java.util.TimerTask prior to jumping on using sleep method.

Conclusion

We learned about two sleep methods implementations with help of two examples and understood the use cases of a sleep method and benefits of sleep method.