Class TaskScheduler

java.lang.Object
is.codion.common.scheduler.TaskScheduler

public final class TaskScheduler extends Object
A task scheduler based on a ScheduledExecutorService, scheduled at a fixed rate, using a daemon thread by default. A TaskScheduler can be stopped and restarted.
 TaskScheduler scheduler = builder()
     .task(() -> System.out.println("Running wild..."))
     .interval(2, TimeUnit.SECONDS)
     .build();

 scheduler.start();
 // ...
 scheduler.interval().set(1);//task restarted using the new interval
 // ...
 scheduler.stop();
See Also:
  • Method Details

    • interval

      public Value<Integer> interval()
      Controls the task interval and when set, in case this scheduler was running, re-schedules the task. If the scheduler was stopped it will remain so, the new interval coming into effect on next start.
      Returns:
      the Value controlling the interval
    • timeUnit

      public TimeUnit timeUnit()
      Returns:
      the time unit
    • start

      public TaskScheduler start()
      Starts this TaskScheduler, if it is running it is restarted, using the initial delay specified during construction.
      Returns:
      this TaskScheduler instance
    • stop

      public void stop()
      Stops this TaskScheduler, if it is not running calling this method has no effect.
    • running

      public boolean running()
      Returns:
      true if this TaskScheduler is running
    • builder

      public static TaskScheduler.Builder.TaskStep builder()
      Returns:
      a new TaskScheduler.Builder.TaskStep instance.