Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@NullMarked @FunctionalInterface public interface Scheduler
A scheduler that submits a task to an executor after a given delay.
  • Method Details

    • schedule

      Future<? extends @Nullable Object> schedule(Executor executor, Runnable command, long delay, TimeUnit unit)
      Returns a future that will submit the task to the executor after the given delay.
      Parameters:
      executor - the executor to run the task
      command - the runnable task to schedule
      delay - how long to delay, in units of unit
      unit - a TimeUnit determining how to interpret the delay parameter
      Returns:
      a scheduled future representing the pending submission of the task
    • disabledScheduler

      static Scheduler disabledScheduler()
      Returns a scheduler that always returns a successfully completed future.
      Returns:
      a scheduler that always returns a successfully completed future
    • systemScheduler

      static Scheduler systemScheduler()
      Returns a scheduler that uses the system-wide scheduling thread by using CompletableFuture.delayedExecutor(long, java.util.concurrent.TimeUnit, java.util.concurrent.Executor).
      Returns:
      a scheduler that uses the system-wide scheduling thread
    • forScheduledExecutorService

      static Scheduler forScheduledExecutorService(ScheduledExecutorService scheduledExecutorService)
      Returns a scheduler that delegates to the a ScheduledExecutorService.

      Note that this implementation will ignore scheduling the task if the executor was shutdown or the submission was rejected. Consider implementing your own adapter if different behavior is required.

      Parameters:
      scheduledExecutorService - the executor to schedule on
      Returns:
      a scheduler that delegates to the a ScheduledExecutorService
    • guardedScheduler

      static Scheduler guardedScheduler(Scheduler scheduler)
      Returns a scheduler that suppresses and logs any exception thrown by the delegate scheduler.
      Parameters:
      scheduler - the scheduler to delegate to
      Returns:
      a scheduler that suppresses and logs any exception thrown by the delegate