HOWTO · Java
How to Delay Few Seconds in Java
Learn when to use Thread.sleep() for a blocking pause and when to schedule delayed or repeating work in Java.
On this page
For a simple pause, call Thread.sleep(); it suspends the current thread for the requested duration. If work should run later or repeat while the calling thread continues, use a ScheduledExecutorService. The examples below show both choices, how to handle interruption, and how to clean up scheduled work.
Pausing execution belongs to the utilities group in the Java Tutorial, with the other everyday date, random, and console recipes.
Use Thread.sleep() for a simple blocking delay
Thread.sleep() accepts a duration in milliseconds and pauses the thread that calls it. This standalone program waits for one second before printing its message. Its main method declares InterruptedException, so interruption is passed to the caller rather than silently discarded.
public class Main {
public static void main(String[] args) throws InterruptedException {
Thread.sleep(1000);
System.out.println("One second has passed.");
}
}
In a method that cannot pass InterruptedException to its caller, catch it and restore the interrupt status with Thread.currentThread().interrupt(). Higher-level code can then observe the cancellation request and respond according to the application’s policy. A sleep pauses only the current thread; it does not pause other threads in the process.
Use TimeUnit.sleep() to express the delay unit
TimeUnit.SECONDS.sleep(2) expresses a two-second pause directly. It still blocks the calling thread and can be interrupted, so apply the same interruption policy as with Thread.sleep().
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) {
try {
TimeUnit.SECONDS.sleep(2);
System.out.println("Two seconds have passed.");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("Pause interrupted.");
}
}
}
Choose this form when naming the unit makes the duration easier to understand. TimeUnit.sleep() is a readable way to express a timed wait, not a way to keep the current thread available for other work while the delay passes.
Schedule a repeating task with ScheduledExecutorService
A ScheduledExecutorService runs a Runnable after a delay or at regular intervals while the calling thread remains available. scheduleAtFixedRate() takes the task, an initial delay, a period, and a TimeUnit. The executor invokes the task’s run() method; run() contains the work and does not schedule itself.
This program starts a task after one second and requests another run every second. A separate scheduled action cancels the repeating task and shuts down the executor after five seconds. Scheduling depends on runtime availability, so the number and timing of printed lines can vary.
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) {
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
ScheduledFuture<?> task = executor.scheduleAtFixedRate(
() -> System.out.println("Task running"), 1, 1, TimeUnit.SECONDS);
executor.schedule(() -> {
task.cancel(false);
executor.shutdown();
}, 5, TimeUnit.SECONDS);
}
}
Keep the returned ScheduledFuture when the repeating task may need cancellation. Shut down an executor when the application no longer needs it; otherwise its worker thread can keep the process alive. For one delayed action, use schedule() instead of a repeating schedule.
Use a method reference with ScheduledExecutorService
When an existing no-argument method represents the task, a method reference supplies it as the Runnable. This program schedules one call two seconds later and then shuts down the executor. Shutdown prevents new tasks from being submitted while allowing the already queued task to finish.
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) {
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.schedule(Main::executeTask, 2, TimeUnit.SECONDS);
executor.shutdown();
}
private static void executeTask() {
System.out.println("Task running");
}
}
Use a sleep method for a short pause when stopping the current thread is intended. Choose a scheduled executor when other work should continue or when a task must run later or repeatedly. Sleep blocks the calling thread and needs no executor cleanup; scheduled work leaves that thread available, but the executor should be shut down and repeating tasks cancelled when they are no longer needed.