Add a delay in Java
This post will discuss how to add a delay in Java for a few seconds.
1. Using ScheduledExecutorService
The idea is to get an executor that can schedule commands to run after a given initial delay or to run periodically. To execute a task once after a delay, use the schedule() method. For example, the following code invokes the run() method after the initial delay of 1 second.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class Main { private static void run() { System.out.println("Running"); } public static void main(String[] args) { ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); executorService.schedule(() -> run(), 1, TimeUnit.SECONDS); } } |
Output:
Running
To schedule a task to execute periodically, use the scheduleAtFixedRate() method. For example, the following code invokes the run() method every 1 second.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class Main { private static void run() { System.out.println("Running"); } public static void main(String[] args) { ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); executorService.scheduleAtFixedRate(() -> run(), 0, 1, TimeUnit.SECONDS); } } |
Output:
Running
Running
…
…
You can also use the scheduleWithFixedDelay() method. Here’s an equivalent version that works with Java 7 and less:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class Main { private static void run() { System.out.println("Running"); } public static void main(String[] args) { ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); executorService.scheduleAtFixedRate(new Runnable() { @Override public void run() { Main.run(); } }, 0, 1, TimeUnit.SECONDS); } } |
Output:
Running
Running
…
…
2. Using sleep() method
A common solution to temporarily cease execution of the current thread is using the Thread.sleep(ms) method. It causes the thread to sleep for the specified number of milliseconds.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class Main { public static void wait(int ms) { try { Thread.sleep(ms); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } } public static void main(String[] args) { System.out.println("Start…"); wait(1000); System.out.println("1 second elapsed…"); } } |
Output:
Start…
1 second elapsed…
Alternatively, you can use the convenience method unit.sleep(sleepFor) that converts time arguments into the form required by the Thread.sleep() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.concurrent.TimeUnit; public class Main { public static void wait(int ms) { try { TimeUnit.MILLISECONDS.sleep(ms); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } } public static void main(String[] args) { System.out.println("Start…"); wait(1000); System.out.println("1 second elapsed…"); } } |
Output:
Start…
1 second elapsed…
3. Using Guava
Note that the sleep() method throws an InterruptedException if the thread is interrupted while sleeping. To invoke unit.sleep(sleepFor) uninterruptibly, consider using the Uninterruptibles.sleepUninterruptibly() method offered by Guava.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import com.google.common.util.concurrent.Uninterruptibles; import java.util.concurrent.TimeUnit; public class Main { public static void wait(int ms) { Uninterruptibles.sleepUninterruptibly(ms, TimeUnit.MILLISECONDS); } public static void main(String[] args) { System.out.println("Start…"); wait(1000); System.out.println("1 second elapsed…"); } } |
Output:
Start…
1 second elapsed…
That’s all about adding a delay in Java for a few seconds.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)