Interface Scheduler
-
- All Known Implementing Classes:
DisabledScheduler
,ExecutorServiceScheduler
,GuardedScheduler
,SystemScheduler
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
@FunctionalInterface public interface Scheduler
A scheduler that submits a task to an executor after a given delay.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description static @NonNull Scheduler
disabledScheduler()
Returns a scheduler that always returns a successfully completed future.static @NonNull Scheduler
forScheduledExecutorService(@NonNull java.util.concurrent.ScheduledExecutorService scheduledExecutorService)
Returns a scheduler that delegates to the aScheduledExecutorService
.static @NonNull Scheduler
guardedScheduler(@NonNull Scheduler scheduler)
Returns a scheduler that suppresses and logs any exception thrown by the delegatescheduler
.@NonNull java.util.concurrent.Future<?>
schedule(@NonNull java.util.concurrent.Executor executor, @NonNull java.lang.Runnable command, @org.checkerframework.checker.index.qual.Positive long delay, @NonNull java.util.concurrent.TimeUnit unit)
Returns a future that will submit the task to the given executor after the given delay.static @NonNull Scheduler
systemScheduler()
Returns a scheduler that uses the system-wide scheduling thread if available, or else returnsdisabledScheduler()
if not present.
-
-
-
Method Detail
-
schedule
@NonNull java.util.concurrent.Future<?> schedule(@NonNull java.util.concurrent.Executor executor, @NonNull java.lang.Runnable command, @org.checkerframework.checker.index.qual.Positive long delay, @NonNull java.util.concurrent.TimeUnit unit)
Returns a future that will submit the task to the given executor after the given delay.- Parameters:
executor
- the executor to run the taskcommand
- the runnable task to scheduledelay
- how long to delay, in units ofunit
unit
- aTimeUnit
determining how to interpret thedelay
parameter- Returns:
- a scheduled future representing pending submission of the task
-
disabledScheduler
static @NonNull Scheduler disabledScheduler()
Returns a scheduler that always returns a successfully completed future.- Returns:
- a scheduler that always returns a successfully completed future
-
systemScheduler
static @NonNull Scheduler systemScheduler()
Returns a scheduler that uses the system-wide scheduling thread if available, or else returnsdisabledScheduler()
if not present. This scheduler is provided in Java 9 or above by usingCompletableFuture
delayedExecutor
.- Returns:
- a scheduler that uses the system-wide scheduling thread if available, or else a disabled scheduler
-
forScheduledExecutorService
static @NonNull Scheduler forScheduledExecutorService(@NonNull java.util.concurrent.ScheduledExecutorService scheduledExecutorService)
Returns a scheduler that delegates to the aScheduledExecutorService
.- Parameters:
scheduledExecutorService
- the executor to schedule on- Returns:
- a scheduler that delegates to the a
ScheduledExecutorService
-
guardedScheduler
static @NonNull Scheduler guardedScheduler(@NonNull Scheduler scheduler)
Returns a scheduler that suppresses and logs any exception thrown by the delegatescheduler
.- Parameters:
scheduler
- the scheduler to delegate to- Returns:
- an scheduler that suppresses and logs any exception thrown by the delegate
-
-