Interface AdvancedScheduledExecutorService

All Superinterfaces:
Executor, ExecutorService, ScheduledExecutorService
All Known Implementing Classes:
AdvancedScheduledThreadPoolExecutor, MonitoredScheduledThreadPoolExecutor

public interface AdvancedScheduledExecutorService extends ScheduledExecutorService
An ScheduledExecutorService that can support advanced commands likes repeats count, auto cancel conditions and run conditions.

The scheduleAdvanced methods create tasks using advanced config ScheduleConfig which defines advance behaviour of task and returns a task object that can be used to cancel or check execution. If specified in ScheduleConfig task will repeat until cancelled or config conditions are met.

All other methods work the same way they work in ScheduledExecutorService.

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 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.

Usage Example

Here is a class with a method that sets up a AdvancedScheduledExecutorService 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");
     scheduler.scheduleAdvanced(beeper, ScheduleConfig.builder()
                         .period(Duration.ofSeconds(10))
                         .repeatCount(360)
                 .createSchedulerConfig());
   }
 }
  • Method Details

    • scheduleAdvanced

      ScheduledFuture<?> scheduleAdvanced(Runnable command, ScheduleConfig<?> config)
      Submits an action with advanced config that becomes enabled first after the given initial delay, and subsequently with the given period or with given time after previous execution. Repeats count may be limited if provided such information in config.

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

      • Repeat count are reached
      • Condition of specified predicate for cancellation was met
      • The task is explicitly cancelled 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 on the returned future will throw ExecutionException, holding the exception as its cause.
      Subsequent executions are suppressed. Subsequent calls to 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.

      If task is not executed due to provided in config predicate, then repeat count does not go up. Repeats are only counted for execution that took place. So if task is specified to repeat 5 time, it will be run 5 times. For example: if task is scheduled to repeat each 10 second 5 times, and will execute 3 times, then execution predicate will prevent it from execution for 1h. The remaining 2 times will be executed after this 1h.

      Parameters:
      command - the task to execute
      config - the config of task
      Returns:
      a ScheduledFuture representing pending completion of the series of repeated tasks. The future's get() method will never return normally, and will throw an exception upon task cancellation or abnormal termination of a task execution.
      Throws:
      RejectedExecutionException - if the task cannot be scheduled for execution
      NullPointerException - if command or config is null
    • scheduleAdvanced

      <T> ScheduledFuture<T> scheduleAdvanced(Callable<T> command, ScheduleConfig<T> config)
      Submits an action with advanced config that becomes enabled first after the given initial delay, and subsequently with the given period or with given time after previous execution. Repeats count may be limited if provided such information in config.

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

      • Repeat count are reached
      • Condition of specified predicate for cancellation was met
      • The task is explicitly cancelled 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 on the returned future will throw ExecutionException, holding the exception as its cause.
      Subsequent executions are suppressed. Subsequent calls to 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.

      If task is not executed due to provided in config predicate, then repeat count does not go up. Repeats are only counted for execution that took place. So if task is specified to repeat 5 time, it will be run 5 times. For example: if task is scheduled to repeat each 10 second 5 times, and will execute 3 times, then execution predicate will prevent it from execution for 1h. The remaining 2 times will be executed after this 1h.

      Parameters:
      command - the task to execute
      config - the config of task
      Returns:
      a ScheduledFuture representing pending completion of the series of repeated tasks. The future's get() method will never return normally, and will throw an exception upon task cancellation or abnormal termination of a task execution.
      Throws:
      RejectedExecutionException - if the task cannot be scheduled for execution
      NullPointerException - if command or config is null