Package org.reactfx

Class ScheduledExecutorServiceTimer

  • All Implemented Interfaces:
    Timer

    class ScheduledExecutorServiceTimer
    extends java.lang.Object
    implements Timer
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.Runnable action  
      private java.util.concurrent.Executor eventThreadExecutor  
      private java.util.concurrent.ScheduledFuture<?> pendingTimer  
      private TriFunction<java.lang.Long,​java.util.concurrent.TimeUnit,​java.lang.Runnable,​java.util.concurrent.ScheduledFuture<?>> scheduler  
      private long seq  
      private long timeout  
      private java.util.concurrent.TimeUnit unit  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ScheduledExecutorServiceTimer​(java.time.Duration timeout, java.lang.Runnable action, TriFunction<java.lang.Long,​java.util.concurrent.TimeUnit,​java.lang.Runnable,​java.util.concurrent.ScheduledFuture<?>> scheduler, java.util.concurrent.Executor eventThreadExecutor)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static Timer create​(java.time.Duration timeout, java.lang.Runnable action, java.util.concurrent.ScheduledExecutorService scheduler, java.util.concurrent.Executor eventThreadExecutor)  
      static Timer createPeriodic​(java.time.Duration timeout, java.lang.Runnable action, java.util.concurrent.ScheduledExecutorService scheduler, java.util.concurrent.Executor eventThreadExecutor)  
      void restart()
      Schedules the associated action to be executed after the associated delay.
      void stop()
      If the associated action has been scheduled for execution but not yet executed, this method prevents it from being executed at all.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • timeout

        private final long timeout
      • unit

        private final java.util.concurrent.TimeUnit unit
      • action

        private final java.lang.Runnable action
      • scheduler

        private final TriFunction<java.lang.Long,​java.util.concurrent.TimeUnit,​java.lang.Runnable,​java.util.concurrent.ScheduledFuture<?>> scheduler
      • eventThreadExecutor

        private final java.util.concurrent.Executor eventThreadExecutor
      • pendingTimer

        private java.util.concurrent.ScheduledFuture<?> pendingTimer
      • seq

        private long seq
    • Constructor Detail

      • ScheduledExecutorServiceTimer

        private ScheduledExecutorServiceTimer​(java.time.Duration timeout,
                                              java.lang.Runnable action,
                                              TriFunction<java.lang.Long,​java.util.concurrent.TimeUnit,​java.lang.Runnable,​java.util.concurrent.ScheduledFuture<?>> scheduler,
                                              java.util.concurrent.Executor eventThreadExecutor)
    • Method Detail

      • create

        public static Timer create​(java.time.Duration timeout,
                                   java.lang.Runnable action,
                                   java.util.concurrent.ScheduledExecutorService scheduler,
                                   java.util.concurrent.Executor eventThreadExecutor)
      • createPeriodic

        public static Timer createPeriodic​(java.time.Duration timeout,
                                           java.lang.Runnable action,
                                           java.util.concurrent.ScheduledExecutorService scheduler,
                                           java.util.concurrent.Executor eventThreadExecutor)
      • restart

        public final void restart()
        Description copied from interface: Timer
        Schedules the associated action to be executed after the associated delay. If the action is already scheduled but hasn't been executed yet, the timeout is reset, so that the action won't be executed before the full delay from now.
        Specified by:
        restart in interface Timer
      • stop

        public final void stop()
        Description copied from interface: Timer
        If the associated action has been scheduled for execution but not yet executed, this method prevents it from being executed at all. This is also true in case the timer's timeout has already expired, but the associated action hasn't had a chance to be executed on the associated thread. Note that this is a stronger guarantee than the one given by Animation.stop():
         
         Timeline timeline = new Timeline(new KeyFrame(
                 Duration.millis(1000),
                 ae -> System.out.println("FIRED ANYWAY")));
         timeline.play();
        
         // later on the JavaFX application thread,
         // but still before the action has been executed
         timeline.stop();
        
         // later, "FIRED ANYWAY" may still be printed
         
         
        In contrast, using the FxTimer, the action is guaranteed not to be executed after stop():
         
         Timer timer = FxTimer.runLater(
                 Duration.ofMillis(1000),
                 () -> System.out.println("FIRED"));
        
         // later on the JavaFX application thread,
         // but still before the action has been executed
         timer.stop();
        
         // "FIRED" is guaranteed *not* to be printed
         
         
        Specified by:
        stop in interface Timer