In this post, we will see how to delay java program for few secs.
We need to delay a java programs in many situation where we want to wait for some other task to finish.
There are multiple ways to delay execution of java program.
- Looking for ⚒️ tech jobs? Go to our job portal.
- Looking for tech events? Go to tech events 🗓️ Calendar.️
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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:
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
To sleep for 10 sec, you can use following code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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:
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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
…
…