Java wait seconds or delay Java program for few secs

Delay java program by few seconds

In this post, we will see how to delay java program for few secs or wait for seconds for java program to proceed further.

We need to delay a java programs in many situation where we want to wait for some other task to finish.
[toc]

There are multiple ways to delay execution of java program or wait for seconds to execute it further.


Using Thread.sleep

Sleep method causes current thread to pause for specific duration of time.We can use Thread’s class sleep static method delay execution of java program.

Here is sample code to the same.

package org.arpit.java2blog;

public class DelayFewSecondsJava {

    public static void main(String[] args) {

        System.out.println("Going for sleep for 5 secs");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Resumed after 5 secs");

    }

}

Output:

Going for sleep for 5 secs
Resumed after 5 secs

Please note that Unit of time is milliseconds for sleep method, that’s why we have passed 5000 for 5 secs delay.

Sleep method is not always accurate as it depends on system timers and schedulers.


Using TimeUnit.XXX.sleep method

You can use java.util.concurrent.TimeUnit to sleep for specific duration of time.

For example:
To sleep for 5 mins, you can use following code

TimeUnit.MINUTES.sleep(5)

To sleep for 10 sec, you can use following code

TimeUnit.SECONDS.sleep(10)
package org.arpit.java2blog;

import java.util.concurrent.TimeUnit;

public class DelayFewSecondsJava {

    public static void main(String[] args) {

        System.out.println("Going for sleep for 10 secs");

        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("Resumed after 10 secs");
    }
}

Output:

Going for sleep for 10 secs
Resumed after 10 secs

Internally TimeUnit.SECONDS.sleep will call Thread.sleep method. Only difference is readability.


Using ScheduledExecutorService

You can also use ScheduledExecutorService which is part of executor framework. This is most precise and powerful solution to pause any java program.

I would strongly recommend to use ScheduledExecutorService in case you want to run the task every secs or few secs delay.

For example:
To run the task every second, you can use following code.

package org.arpit.java2blog;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class DelayFewSecondsJava {

    public static void main(String[] args) {

         ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
         executorService.scheduleAtFixedRate(DelayFewSecondsJava::runTask, 0, 1, TimeUnit.SECONDS);
    }

    public static void runTask()
    {
        System.out.println("Running the task each second");
    }
}

executorService.scheduleAtFixedRate(DelayFewSecondsJava::runTask, 0, 1, TimeUnit.SECONDS);
Here DelayFewSecondsJava is classname and runTask is method name of that class.

Output:

Running the task each second
Running the task each second
Running the task each second
Running the task each second
…
…

Frequently asked questions on Java wait seconds

How to wait for 5 seconds in java?

You can simply use below code to wait for 5 seconds.

Thread.sleep(5000)
or 
TimeUnit.MINUTES.sleep(5)

How to wait for 1 seconds in java?

You can simply use below code to wait for 1 seconds.

Thread.sleep(1000)
or 
TimeUnit.MINUTES.sleep(1)

How to pause for 5 seconds in java?

Pause and wait are synonyms here, so you can simply use below code to pause for 5 seconds.

Thread.sleep(5000)
or 
TimeUnit.MINUTES.sleep(5)

That’s all how to delay java program for few seconds or how to wait for seconds in java.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.