# ScheduledExecutorService

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary: [Methods](https://developer.android.com/reference/java/util/concurrent/ScheduledExecutorService#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/java/util/concurrent/ScheduledExecutorService#inhmethods)  

# ScheduledExecutorService


`
public


interface
ScheduledExecutorService
`


`


implements

`[ExecutorService](https://developer.android.com/reference/java/util/concurrent/ExecutorService)`


`

|-----------------------------------------------|
| java.util.concurrent.ScheduledExecutorService |

|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Known indirect subclasses [ScheduledThreadPoolExecutor](https://developer.android.com/reference/java/util/concurrent/ScheduledThreadPoolExecutor) |-------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | [ScheduledThreadPoolExecutor](https://developer.android.com/reference/java/util/concurrent/ScheduledThreadPoolExecutor) | A [ThreadPoolExecutor](https://developer.android.com/reference/java/util/concurrent/ThreadPoolExecutor) that can additionally schedule commands to run after a given delay, or to execute periodically. | |

<br />

*** ** * ** ***

An [ExecutorService](https://developer.android.com/reference/java/util/concurrent/ExecutorService) that can schedule commands to run after a given
delay, or to execute periodically.

The `schedule` methods create tasks with various delays
and return a task object that can be used to cancel or check
execution. The `scheduleAtFixedRate` and
`scheduleWithFixedDelay` methods create and execute tasks
that run periodically until cancelled.

Commands submitted using the [Executor.execute(Runnable)](https://developer.android.com/reference/java/util/concurrent/Executor#execute(java.lang.Runnable))
and [ExecutorService](https://developer.android.com/reference/java/util/concurrent/ExecutorService) `submit` methods are scheduled
with a requested delay of zero. Zero and negative delays (but not
periods) are also allowed in `schedule` methods, and are
treated as requests for immediate execution.

All `schedule` methods accept *relative* delays and
periods as arguments, not absolute times or dates. It is a simple
matter to transform an absolute time represented as a [Date](https://developer.android.com/reference/java/util/Date) to the required form. For example, to schedule at
a certain future `date`, you can use: `schedule(task,
date.getTime() - System.currentTimeMillis(),
TimeUnit.MILLISECONDS)`. Beware however that expiration of a
relative delay need not coincide with the current `Date` at
which the task is enabled due to network time synchronization
protocols, clock drift, or other factors.

The [Executors](https://developer.android.com/reference/java/util/concurrent/Executors) class provides convenient factory methods for
the ScheduledExecutorService implementations provided in this package.

## Usage Example

Here is a class with a method that sets up a ScheduledExecutorService to beep every ten seconds for an hour:  

     import static java.util.concurrent.TimeUnit.*;
     class BeeperControl {
       private final ScheduledExecutorService scheduler =
         Executors.newScheduledThreadPool(1);

       public void beepForAnHour() {
         Runnable beeper = () -> System.out.println("beep");
         ScheduledFuture<?> beeperHandle =
           scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
         Runnable canceller = () -> beeperHandle.cancel(false);
         scheduler.schedule(canceller, 1, HOURS);
       }
     }

<br />

## Summary

|                                                                                                                                                                                                                                                                                                                                                                                              ### Public methods                                                                                                                                                                                                                                                                                                                                                                                              ||
|----------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` abstract <V> `[ScheduledFuture](https://developer.android.com/reference/java/util/concurrent/ScheduledFuture)`<V>` | ` `[schedule](https://developer.android.com/reference/java/util/concurrent/ScheduledExecutorService#schedule(java.util.concurrent.Callable<V>,%20long,%20java.util.concurrent.TimeUnit))`(`[Callable](https://developer.android.com/reference/java/util/concurrent/Callable)`<V> callable, long delay, `[TimeUnit](https://developer.android.com/reference/java/util/concurrent/TimeUnit)` unit) ` Submits a value-returning one-shot task that becomes enabled after the given delay.                                                                                                                                                                                                 |
| ` abstract `[ScheduledFuture](https://developer.android.com/reference/java/util/concurrent/ScheduledFuture)`<?>`     | ` `[schedule](https://developer.android.com/reference/java/util/concurrent/ScheduledExecutorService#schedule(java.lang.Runnable,%20long,%20java.util.concurrent.TimeUnit))`(`[Runnable](https://developer.android.com/reference/java/lang/Runnable)` command, long delay, `[TimeUnit](https://developer.android.com/reference/java/util/concurrent/TimeUnit)` unit) ` Submits a one-shot task that becomes enabled after the given delay.                                                                                                                                                                                                                                              |
| ` abstract `[ScheduledFuture](https://developer.android.com/reference/java/util/concurrent/ScheduledFuture)`<?>`     | ` `[scheduleAtFixedRate](https://developer.android.com/reference/java/util/concurrent/ScheduledExecutorService#scheduleAtFixedRate(java.lang.Runnable,%20long,%20long,%20java.util.concurrent.TimeUnit))`(`[Runnable](https://developer.android.com/reference/java/lang/Runnable)` command, long initialDelay, long period, `[TimeUnit](https://developer.android.com/reference/java/util/concurrent/TimeUnit)` unit) ` Submits a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; that is, executions will commence after `initialDelay`, then `initialDelay + period`, then `initialDelay + 2 * period`, and so on. |
| ` abstract `[ScheduledFuture](https://developer.android.com/reference/java/util/concurrent/ScheduledFuture)`<?>`     | ` `[scheduleWithFixedDelay](https://developer.android.com/reference/java/util/concurrent/ScheduledExecutorService#scheduleWithFixedDelay(java.lang.Runnable,%20long,%20long,%20java.util.concurrent.TimeUnit))`(`[Runnable](https://developer.android.com/reference/java/lang/Runnable)` command, long initialDelay, long delay, `[TimeUnit](https://developer.android.com/reference/java/util/concurrent/TimeUnit)` unit) ` Submits a periodic action that becomes enabled first after the given initial delay, and subsequently with the given delay between the termination of one execution and the commencement of the next.                                                      |

| ### Inherited methods |
|-----------------------|---|
| From interface ` `[java.util.concurrent.ExecutorService](https://developer.android.com/reference/java/util/concurrent/ExecutorService)` ` |----------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract boolean`                                                                                                                                                  | ` `[awaitTermination](https://developer.android.com/reference/java/util/concurrent/ExecutorService#awaitTermination(long,%20java.util.concurrent.TimeUnit))`(long timeout, `[TimeUnit](https://developer.android.com/reference/java/util/concurrent/TimeUnit)` unit) ` Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.                                                                                                                                                                                                                                                          | | ` default void`                                                                                                                                                      | ` `[close](https://developer.android.com/reference/java/util/concurrent/ExecutorService#close())`() ` Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.                                                                                                                                                                                                                                                                                                                                                                                                                                                                        | | ` abstract <T> `[List](https://developer.android.com/reference/java/util/List)`<`[Future](https://developer.android.com/reference/java/util/concurrent/Future)`<T>>` | ` `[invokeAll](https://developer.android.com/reference/java/util/concurrent/ExecutorService#invokeAll(java.util.Collection<?%20extends%20java.util.concurrent.Callable<T>>))`(`[Collection](https://developer.android.com/reference/java/util/Collection)`<? extends `[Callable](https://developer.android.com/reference/java/util/concurrent/Callable)`<T>> tasks) ` Executes the given tasks, returning a list of Futures holding their status and results when all complete.                                                                                                                                                                                                                 | | ` abstract <T> `[List](https://developer.android.com/reference/java/util/List)`<`[Future](https://developer.android.com/reference/java/util/concurrent/Future)`<T>>` | ` `[invokeAll](https://developer.android.com/reference/java/util/concurrent/ExecutorService#invokeAll(java.util.Collection<?%20extends%20java.util.concurrent.Callable<T>>,%20long,%20java.util.concurrent.TimeUnit))`(`[Collection](https://developer.android.com/reference/java/util/Collection)`<? extends `[Callable](https://developer.android.com/reference/java/util/concurrent/Callable)`<T>> tasks, long timeout, `[TimeUnit](https://developer.android.com/reference/java/util/concurrent/TimeUnit)` unit) ` Executes the given tasks, returning a list of Futures holding their status and results when all complete or the timeout expires, whichever happens first.                | | ` abstract <T> T`                                                                                                                                                    | ` `[invokeAny](https://developer.android.com/reference/java/util/concurrent/ExecutorService#invokeAny(java.util.Collection<?%20extends%20java.util.concurrent.Callable<T>>))`(`[Collection](https://developer.android.com/reference/java/util/Collection)`<? extends `[Callable](https://developer.android.com/reference/java/util/concurrent/Callable)`<T>> tasks) ` Executes the given tasks, returning the result of one that has completed successfully (i.e., without throwing an exception), if any do.                                                                                                                                                                                   | | ` abstract <T> T`                                                                                                                                                    | ` `[invokeAny](https://developer.android.com/reference/java/util/concurrent/ExecutorService#invokeAny(java.util.Collection<?%20extends%20java.util.concurrent.Callable<T>>,%20long,%20java.util.concurrent.TimeUnit))`(`[Collection](https://developer.android.com/reference/java/util/Collection)`<? extends `[Callable](https://developer.android.com/reference/java/util/concurrent/Callable)`<T>> tasks, long timeout, `[TimeUnit](https://developer.android.com/reference/java/util/concurrent/TimeUnit)` unit) ` Executes the given tasks, returning the result of one that has completed successfully (i.e., without throwing an exception), if any do before the given timeout elapses. | | ` abstract boolean`                                                                                                                                                  | ` `[isShutdown](https://developer.android.com/reference/java/util/concurrent/ExecutorService#isShutdown())`() ` Returns `true` if this executor has been shut down.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             | | ` abstract boolean`                                                                                                                                                  | ` `[isTerminated](https://developer.android.com/reference/java/util/concurrent/ExecutorService#isTerminated())`() ` Returns `true` if all tasks have completed following shut down.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             | | ` abstract void`                                                                                                                                                     | ` `[shutdown](https://developer.android.com/reference/java/util/concurrent/ExecutorService#shutdown())`() ` Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.                                                                                                                                                                                                                                                                                                                                                                                                                                                                  | | ` abstract `[List](https://developer.android.com/reference/java/util/List)`<`[Runnable](https://developer.android.com/reference/java/lang/Runnable)`>`               | ` `[shutdownNow](https://developer.android.com/reference/java/util/concurrent/ExecutorService#shutdownNow())`() ` Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.                                                                                                                                                                                                                                                                                                                                                                                                                           | | ` abstract `[Future](https://developer.android.com/reference/java/util/concurrent/Future)`<?>`                                                                       | ` `[submit](https://developer.android.com/reference/java/util/concurrent/ExecutorService#submit(java.lang.Runnable))`(`[Runnable](https://developer.android.com/reference/java/lang/Runnable)` task) ` Submits a Runnable task for execution and returns a Future representing that task.                                                                                                                                                                                                                                                                                                                                                                                                       | | ` abstract <T> `[Future](https://developer.android.com/reference/java/util/concurrent/Future)`<T>`                                                                   | ` `[submit](https://developer.android.com/reference/java/util/concurrent/ExecutorService#submit(java.util.concurrent.Callable<T>))`(`[Callable](https://developer.android.com/reference/java/util/concurrent/Callable)`<T> task) ` Submits a value-returning task for execution and returns a Future representing the pending results of the task.                                                                                                                                                                                                                                                                                                                                              | | ` abstract <T> `[Future](https://developer.android.com/reference/java/util/concurrent/Future)`<T>`                                                                   | ` `[submit](https://developer.android.com/reference/java/util/concurrent/ExecutorService#submit(java.lang.Runnable,%20T))`(`[Runnable](https://developer.android.com/reference/java/lang/Runnable)` task, T result) ` Submits a Runnable task for execution and returns a Future representing that task.                                                                                                                                                                                                                                                                                                                                                                                        | ||
| From interface ` `[java.lang.AutoCloseable](https://developer.android.com/reference/java/lang/AutoCloseable)` ` |------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract void` | ` `[close](https://developer.android.com/reference/java/lang/AutoCloseable#close())`() ` Closes this resource, relinquishing any underlying resources. | ||
| From interface ` `[java.util.concurrent.Executor](https://developer.android.com/reference/java/util/concurrent/Executor)` ` |------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract void` | ` `[execute](https://developer.android.com/reference/java/util/concurrent/Executor#execute(java.lang.Runnable))`(`[Runnable](https://developer.android.com/reference/java/lang/Runnable)` command) ` Executes the given command at some time in the future. | ||

## Public methods

### schedule

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public abstract ScheduledFuture<V> schedule (Callable<V> callable, 
                long delay, 
                TimeUnit unit)
```

Submits a value-returning one-shot task that becomes enabled
after the given delay.

<br />

|                             Parameters                              ||
|------------|---------------------------------------------------------|
| `callable` | `Callable`: the function to execute <br />              |
| `delay`    | `long`: the time from now to delay execution <br />     |
| `unit`     | `TimeUnit`: the time unit of the delay parameter <br /> |

|                                                                                   Returns                                                                                   ||
|------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------|
| [ScheduledFuture](https://developer.android.com/reference/java/util/concurrent/ScheduledFuture)`<V>` | a ScheduledFuture that can be used to extract result or cancel <br /> |

|                                                                                Throws                                                                                ||
|-----------------------------------------------------------------------------------------------------------------------|-----------------------------------------------|
| [RejectedExecutionException](https://developer.android.com/reference/java/util/concurrent/RejectedExecutionException) | if the task cannot be scheduled for execution |
| [NullPointerException](https://developer.android.com/reference/java/lang/NullPointerException)                        | if callable or unit is null                   |

### schedule

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public abstract ScheduledFuture<?> schedule (Runnable command, 
                long delay, 
                TimeUnit unit)
```

Submits a one-shot task that becomes enabled after the given delay.

<br />

|                             Parameters                             ||
|-----------|---------------------------------------------------------|
| `command` | `Runnable`: the task to execute <br />                  |
| `delay`   | `long`: the time from now to delay execution <br />     |
| `unit`    | `TimeUnit`: the time unit of the delay parameter <br /> |

|                                                                                                                Returns                                                                                                                 ||
|------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------|
| [ScheduledFuture](https://developer.android.com/reference/java/util/concurrent/ScheduledFuture)`<?>` | a ScheduledFuture representing pending completion of the task and whose `get()` method will return `null` upon completion <br /> |

|                                                                                Throws                                                                                ||
|-----------------------------------------------------------------------------------------------------------------------|-----------------------------------------------|
| [RejectedExecutionException](https://developer.android.com/reference/java/util/concurrent/RejectedExecutionException) | if the task cannot be scheduled for execution |
| [NullPointerException](https://developer.android.com/reference/java/lang/NullPointerException)                        | if command or unit is null                    |

### scheduleAtFixedRate

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public abstract ScheduledFuture<?> scheduleAtFixedRate (Runnable command, 
                long initialDelay, 
                long period, 
                TimeUnit unit)
```

Submits a periodic action that becomes enabled first after the
given initial delay, and subsequently with the given period;
that is, executions will commence after
`initialDelay`, then `initialDelay + period`, then
`initialDelay + 2 * period`, and so on.

The sequence of task executions continues indefinitely until
one of the following exceptional completions occur:

- The task is [explicitly cancelled](https://developer.android.com/reference/java/util/concurrent/Future#cancel(boolean)) via the returned future.
- The executor terminates, also resulting in task cancellation.
- An execution of the task throws an exception. In this case calling [get](https://developer.android.com/reference/java/util/concurrent/Future#get()) on the returned future will throw [ExecutionException](https://developer.android.com/reference/java/util/concurrent/ExecutionException), holding the exception as its cause.

Subsequent executions are suppressed. Subsequent calls to [isDone()](https://developer.android.com/reference/java/util/concurrent/Future#isDone()) on the returned future will return `true`.

If any execution of this task takes longer than its period, then
subsequent executions may start late, but will not concurrently
execute.

<br />

|                                         Parameters                                         ||
|----------------|----------------------------------------------------------------------------|
| `command`      | `Runnable`: the task to execute <br />                                     |
| `initialDelay` | `long`: the time to delay first execution <br />                           |
| `period`       | `long`: the period between successive executions <br />                    |
| `unit`         | `TimeUnit`: the time unit of the initialDelay and period parameters <br /> |

|                                                                                                                                                                                                              Returns                                                                                                                                                                                                               ||
|------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [ScheduledFuture](https://developer.android.com/reference/java/util/concurrent/ScheduledFuture)`<?>` | a ScheduledFuture representing pending completion of the series of repeated tasks. The future's [get()](https://developer.android.com/reference/java/util/concurrent/Future#get()) method will never return normally, and will throw an exception upon task cancellation or abnormal termination of a task execution. <br /> |

|                                                                                Throws                                                                                ||
|-----------------------------------------------------------------------------------------------------------------------|-----------------------------------------------|
| [RejectedExecutionException](https://developer.android.com/reference/java/util/concurrent/RejectedExecutionException) | if the task cannot be scheduled for execution |
| [NullPointerException](https://developer.android.com/reference/java/lang/NullPointerException)                        | if command or unit is null                    |
| [IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException)                | if period less than or equal to zero          |

### scheduleWithFixedDelay

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public abstract ScheduledFuture<?> scheduleWithFixedDelay (Runnable command, 
                long initialDelay, 
                long delay, 
                TimeUnit unit)
```

Submits a periodic action that becomes enabled first after the
given initial delay, and subsequently with the given delay
between the termination of one execution and the commencement of
the next.

The sequence of task executions continues indefinitely until
one of the following exceptional completions occur:

- The task is [explicitly cancelled](https://developer.android.com/reference/java/util/concurrent/Future#cancel(boolean)) via the returned future.
- The executor terminates, also resulting in task cancellation.
- An execution of the task throws an exception. In this case calling [get](https://developer.android.com/reference/java/util/concurrent/Future#get()) on the returned future will throw [ExecutionException](https://developer.android.com/reference/java/util/concurrent/ExecutionException), holding the exception as its cause.

Subsequent executions are suppressed. Subsequent calls to [isDone()](https://developer.android.com/reference/java/util/concurrent/Future#isDone()) on the returned future will return `true`.

<br />

<br />

|                                                     Parameters                                                     ||
|----------------|----------------------------------------------------------------------------------------------------|
| `command`      | `Runnable`: the task to execute <br />                                                             |
| `initialDelay` | `long`: the time to delay first execution <br />                                                   |
| `delay`        | `long`: the delay between the termination of one execution and the commencement of the next <br /> |
| `unit`         | `TimeUnit`: the time unit of the initialDelay and delay parameters <br />                          |

|                                                                                                                                                                                                              Returns                                                                                                                                                                                                               ||
|------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [ScheduledFuture](https://developer.android.com/reference/java/util/concurrent/ScheduledFuture)`<?>` | a ScheduledFuture representing pending completion of the series of repeated tasks. The future's [get()](https://developer.android.com/reference/java/util/concurrent/Future#get()) method will never return normally, and will throw an exception upon task cancellation or abnormal termination of a task execution. <br /> |

|                                                                                Throws                                                                                ||
|-----------------------------------------------------------------------------------------------------------------------|-----------------------------------------------|
| [RejectedExecutionException](https://developer.android.com/reference/java/util/concurrent/RejectedExecutionException) | if the task cannot be scheduled for execution |
| [NullPointerException](https://developer.android.com/reference/java/lang/NullPointerException)                        | if command or unit is null                    |
| [IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException)                | if delay less than or equal to zero           |