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 a ScheduledExecutorService.
      static @NonNull Scheduler guardedScheduler​(@NonNull Scheduler scheduler)
      Returns a scheduler that suppresses and logs any exception thrown by the delegate scheduler.
      @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 returns disabledScheduler() 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 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 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 returns disabledScheduler() if not present. This scheduler is provided in Java 9 or above by using CompletableFuture 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 a ScheduledExecutorService.
        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 delegate scheduler.
        Parameters:
        scheduler - the scheduler to delegate to
        Returns:
        an scheduler that suppresses and logs any exception thrown by the delegate