How to Add Delay in Java For Few Seconds

In this tutorial, I will be sharing how to add a delay in java for a few seconds. Sometimes we need to introduce a delay in the execution of our java program. It can be achieved in 3 ways.

1. Using Thread.sleep() method
2. Using TimeUnit.XXX.sleep() method 
3. Using ScheduledExecutorService  

Let's dive deep into the topic.

Read Also: Multithreading Interview Questions

Add Delay in Java For Few Seconds

1. Using Thread.sleep() method 

The easiest way to delay a java program is by using Thread.sleep() method. The sleep() method is present in the Thread class. It simply pauses the current thread to sleep for a specific time. 
Syntax:

Thread.sleep( time In Milliseconds)


Note: Unit of time for Thread's class static sleep method is in milliseconds. So, in order to delay for 7 seconds, we need to pass 7000 as an argument in the sleep method.


Java Program

import java.util.Date; 
 
public class JavaHungry {
    public static void main(String args[]) {
      try
      {
        System.out.println("Start of delay: "+ new Date());
        // Delay for 7 seonds
        Thread.sleep(7000);   
        System.out.println("End of delay: "+ new Date());
      }
      catch(InterruptedException ex)
      {
          ex.printStackTrace();
      }
    }
}

Output:
Start of delay: Tue Oct 13 09:04:52 GMT 2020
End of delay: Tue Oct 13 09:04:59 GMT 2020

2. Using TimeUnit.XXX.sleep() method


There is also another way to add a delay in the Java program i.e by using TimeUnit's sleep method. 

Syntax:

TimeUnit.SECONDS.sleep(time In Seconds)

TimeUnit.MINUTES.sleep(time In Minutes)

TimeUnit.MILLISECONDS.sleep(time In Milliseconds)

Note: TimeUnit.SECONDS.sleep() method internally calls Thread.sleep() method.

Java Program:

import java.util.concurrent.TimeUnit;
import java.util.Date;

public class JavaHungry {
    public static void main(String args[]) {
      try
      {
        System.out.println("Start of delay: "+ new Date());
        // Delay for 7 seonds
        TimeUnit.SECONDS.sleep(7);   
        System.out.println("End of delay: "+ new Date());
        // Delay for 1 minute
        TimeUnit.MINUTES.sleep(1);
        // Delay for 5000 Milliseconds
        TimeUnit.MILLISECONDS.sleep(5000);
      }
      catch(InterruptedException ex)
      {
          ex.printStackTrace();
      }
    }
}

Output:

Start of delay: Tue Oct 13 09:26:39 GMT 2020
End of delay: Tue Oct 13 09:26:46 GMT 2020

3. Using ScheduledExecutorService

The Executor framework's ScheduledExecutorService interface can also be used to add a delay in the java program for a few seconds. Among all the methods described in this post, this is the most precise way to add delay. We will use the schedule() method to run the piece of code once after a delay.

Java Program:

import java.util.concurrent.TimeUnit;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
 
public class JavaHungry {
    
    public static void main(String args[]) {
        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
        /* schedule(ClassName::anyTask, delayInSeconds, TimeUnit.SECONDS)
           where anyTask() is the name of the method we want to execute and
           ClassName is the class containing the anyTask() method */
        service.schedule(JavaHungry::executeTask, 5, TimeUnit.SECONDS);
    }
    public static void executeTask(){
        System.out.println(" Task is running after 5 seconds");
    }
}

Output:
Task is running after 5 seconds

That's all for today. Please mention in the comments in case you have any questions related to how to add a delay in java for a few seconds.

References:

TimeUnit Oracle doc 

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry