# Add a Delay or Sleep in Java with Thread and Executor

> Pause execution in Java using Thread.sleep for simple delays or ScheduledExecutorService for non-blocking, flexible task scheduling with thread pools

**URL:** https://sentry.io/answers/how-to-add-a-delay-in-java/

---

## The Problem

I don't know how to delay an execution for a specified period of time in Java.

## The Solution

You can use either the `Thread.sleep()` method or the `ScheduledExecutorService` interface to add an execution delay in Java.

### Using `Thread.sleep()`

You can use `Thread.sleep()` to delay the execution of a task by putting a thread to sleep for a specified number of milliseconds.

For example, the following adds a three-second execution delay by putting the thread to sleep for 3,000 milliseconds:

```java
public class Main {
    public static void main(String[] args) {
        System.out.println("Task started");

        try {
            Thread.sleep(3000); 
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("Task completed after 3 seconds delay");
    }
}
```

The `Thread.sleep()` method is simple to implement but may not be suitable for all use cases, as it blocks the current thread and doesn't offer flexible task control. It is best used in situations that require a simple once-off delay within a single thread.

It is important to wrap the `Thread.sleep()` method in a try-catch block to handle the `InterruptedException` that is thrown if the thread is interrupted while sleeping.

### Using `ScheduledExecutorService`

The `ScheduledExecutorService` interface provides more flexible control, allowing you to schedule delayed or periodically executed tasks.

Similar to the `Thread.sleep()` example, the following code delays the execution of a task by three seconds:

```java
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 scheduler = Executors.newScheduledThreadPool(1); // 1 is the size of the thread pool being created

        System.out.println("Scheduling a task with a 3-second delay");

        scheduler.schedule(() -> {
            System.out.println("Task executed after 3 seconds"); // Task to be executed after the delay
        }, 3, TimeUnit.SECONDS); // Adding the 3-second delay

        scheduler.shutdown();
    }
}
```

The task will run after the specified time (3 seconds in this case).

The `ScheduledExecutorService` interface is slightly more complex to implement, but it doesn't block the current thread. It's best used in situations that require more complex scheduling and control.

## Further Reading

- The [Java `Thread` class](https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/lang/Thread.html) documentation
- The [Java `Executors` class](https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/util/concurrent/Executors.html) documentation
- The [Java `ScheduledExecutorService` interface](https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/util/concurrent/ScheduledExecutorService.html) documentation
- The [Java `TimeUnit` class](https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/util/concurrent/TimeUnit.html) documentation

---

*Source: [sentry.io/answers/how-to-add-a-delay-in-java/](https://sentry.io/answers/how-to-add-a-delay-in-java/)*
