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.
A scheduler that submits a task to an executor after a given delay.
-
Method Summary
Modifier and TypeMethodDescriptionstatic @NonNull Scheduler
Returns a scheduler that always returns a successfully completed future.static @NonNull Scheduler
forScheduledExecutorService
(@NonNull 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 Future
<?> schedule
(@NonNull Executor executor, @NonNull Runnable command, @org.checkerframework.checker.index.qual.Positive long delay, @NonNull TimeUnit unit) Returns a future that will submit the task to the given executor after the given delay.static @NonNull Scheduler
Returns a scheduler that uses the system-wide scheduling thread if available, or else returnsdisabledScheduler()
if not present.
-
Method Details
-
schedule
@NonNull Future<?> schedule(@NonNull Executor executor, @NonNull Runnable command, @org.checkerframework.checker.index.qual.Positive long delay, @NonNull 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
Returns a scheduler that always returns a successfully completed future.- Returns:
- a scheduler that always returns a successfully completed future
-
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 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
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
-