Interface TaskScheduler


  • public interface TaskScheduler
    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 = TaskScheduler.builder(() -> System.out.println("Running wild..."))
         .interval(2)
         .timeUnit(TimeUnit.SECONDS)
         .build();
    
     scheduler.start();
     ...
     scheduler.setInterval(1);//task restarted using the new interval
     ...
     scheduler.stop();
     
    See Also:
    builder(Runnable)
    • Method Detail

      • getInterval

        int getInterval()
        Returns:
        the interval
      • setInterval

        void setInterval​(int interval)
        Sets the new task interval and, 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.
        Parameters:
        interval - the interval
        Throws:
        IllegalArgumentException - in case interval isn't a positive integer
      • start

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

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

        boolean isRunning()
        Returns:
        true if this TaskScheduler is running
      • addIntervalListener

        void addIntervalListener​(EventDataListener<Integer> listener)
        Adds a listener notified when the interval is set.
        Parameters:
        listener - a listener notified each time the interval is set