Package jmri.util

Class TimerUtil

java.lang.Object
jmri.util.TimerUtil

@Immutable public final class TimerUtil extends Object
Common utility methods for working with (@link java.util.Timer)

Each Timer uses a thread, which means that they're not throw-away timers: You either track when you can destroy them (and that destruction is not obvious), or they stick around consuming resources.

This class provides most of the functionality of a Timer. Some differences:

  • When migrating code that uses Timer.cancel() to end operation, you have to retain references to the individual TimerTask objects and cancel them instead.

For convenience, this also provides methods to ensure that the task is invoked on a specific JMRI thread.

Please note the comment in the Timer Javadoc about how ScheduledThreadPoolExecutor might provide a better underlying implementation. Method JavaDoc tweaked from java.util.Timer.

  • Field Details

  • Method Details

    • schedule

      public static void schedule(@Nonnull TimerTask task, @Nonnull Date time)
      Schedule a TimerTask for execution at the specified time. If time is in the past, the task is scheduled for immediate execution.
      Parameters:
      task - task to be scheduled.
      time - time at which task is to be executed.
    • schedule

      public static void schedule(@Nonnull TimerTask task, @Nonnull Date firstTime, long period)
      Schedules the specified task for repeated fixed-delay execution, beginning at the specified time. Subsequent executions take place at approximately regular intervals, separated by the specified period.
      Parameters:
      task - task to be scheduled.
      firstTime - First time at which task is to be executed.
      period - time in milliseconds between successive task executions.
    • schedule

      public static void schedule(@Nonnull TimerTask task, long delay)
      Schedules the specified task for execution after the specified delay.
      Parameters:
      task - task to be scheduled.
      delay - delay in milliseconds before task is to be executed.
    • schedule

      public static void schedule(@Nonnull TimerTask task, long delay, long period)
      Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay. Subsequent executions take place at approximately regular intervals separated by the specified period.
      Parameters:
      task - task to be scheduled.
      delay - delay in milliseconds before task is to be executed.
      period - time in milliseconds between successive task executions.
    • scheduleAtFixedRate

      public static void scheduleAtFixedRate(@Nonnull TimerTask task, @Nonnull Date firstTime, long period)
      Schedules the specified task for repeated fixed-delay execution, beginning at the specified time. Subsequent executions take place at approximately regular intervals, separated by the specified period.
      Parameters:
      task - task to be scheduled.
      firstTime - First time at which task is to be executed.
      period - time in milliseconds between successive task executions.
    • scheduleAtFixedRate

      public static void scheduleAtFixedRate(@Nonnull TimerTask task, long delay, long period)
      Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay. Subsequent executions take place at approximately regular intervals separated by the specified period.
      Parameters:
      task - task to be scheduled.
      delay - delay in milliseconds before task is to be executed.
      period - time in milliseconds between successive task executions.
    • scheduleOnGUIThread

      Schedule a TimerTask on GUI Thread for execution at the specified time. If time is in the past, the task is scheduled for immediate execution.
      Parameters:
      task - task to be scheduled.
      time - time at which task is to be executed.
      Returns:
      Actual scheduled task; use this if you need to cancel
    • scheduleOnGUIThread

      @Nonnull public static TimerTask scheduleOnGUIThread(@Nonnull TimerTask task, @Nonnull Date firstTime, long period)
      Schedules the specified task for repeated fixed-delay execution on the GUI Thread, beginning at the specified time. Subsequent executions take place at approximately regular intervals, separated by the specified period.
      Parameters:
      task - task to be scheduled.
      firstTime - First time at which task is to be executed.
      period - time in milliseconds between successive task executions.
      Returns:
      Actual scheduled task; use this if you need to cancel
    • scheduleOnGUIThread

      @Nonnull public static TimerTask scheduleOnGUIThread(@Nonnull TimerTask task, long delay)
      Schedules the specified task for execution on the GUI Thread after the specified delay.
      Parameters:
      task - task to be scheduled.
      delay - delay in milliseconds before task is to be executed.
      Returns:
      Actual scheduled task; use this if you need to cancel
    • scheduleOnGUIThread

      @Nonnull public static TimerTask scheduleOnGUIThread(@Nonnull TimerTask task, long delay, long period)
      Schedules the specified task for repeated fixed-delay execution on the GUI Thread, beginning after the specified delay. Subsequent executions take place at approximately regular intervals separated by the specified period.
      Parameters:
      task - task to be scheduled.
      delay - delay in milliseconds before task is to be executed.
      period - time in milliseconds between successive task executions.
      Returns:
      Actual scheduled task; use this if you need to cancel
    • scheduleAtFixedRateOnGUIThread

      @Nonnull public static TimerTask scheduleAtFixedRateOnGUIThread(@Nonnull TimerTask task, @Nonnull Date firstTime, long period)
      Schedules the specified task for repeated fixed-delay execution, on the GUI Thread, beginning at the specified time. Subsequent executions take place at approximately regular intervals, separated by the specified period.
      Parameters:
      task - task to be scheduled.
      firstTime - First time at which task is to be executed.
      period - time in milliseconds between successive task executions.
      Returns:
      Actual scheduled task; use this if you need to cancel
    • scheduleAtFixedRateOnGUIThread

      @Nonnull public static TimerTask scheduleAtFixedRateOnGUIThread(@Nonnull TimerTask task, long delay, long period)
      Schedules the specified task for repeated fixed-delay execution on the GUI Thread beginning after the specified delay. Subsequent executions take place at approximately regular intervals separated by the specified period.
      Parameters:
      task - task to be scheduled.
      delay - delay in milliseconds before task is to be executed.
      period - time in milliseconds between successive task executions.
      Returns:
      Actual scheduled task; use this if you need to cancel
    • scheduleOnLayoutThread

      public static void scheduleOnLayoutThread(@Nonnull TimerTask task, @Nonnull Date time)
      Schedule a TimerTask on Layout Thread for execution at the specified time. If time is in the past, the task is scheduled for immediate execution.
      Parameters:
      task - task to be scheduled.
      time - time at which task is to be executed.
    • scheduleOnLayoutThread

      public static void scheduleOnLayoutThread(@Nonnull TimerTask task, @Nonnull Date firstTime, long period)
      Schedules the specified task for repeated fixed-delay execution on the Layout Thread, beginning at the specified time. Subsequent executions take place at approximately regular intervals, separated by the specified period.
      Parameters:
      task - task to be scheduled.
      firstTime - First time at which task is to be executed.
      period - time in milliseconds between successive task executions.
    • scheduleOnLayoutThread

      public static void scheduleOnLayoutThread(@Nonnull TimerTask task, long delay)
      Schedules the specified task for execution on the Layout Thread after the specified delay.
      Parameters:
      task - task to be scheduled.
      delay - delay in milliseconds before task is to be executed.
    • scheduleOnLayoutThread

      public static void scheduleOnLayoutThread(@Nonnull TimerTask task, long delay, long period)
      Schedules the specified task for repeated fixed-delay execution on the Layout Thread beginning after the specified delay. Subsequent executions take place at approximately regular intervals separated by the specified period.
      Parameters:
      task - task to be scheduled.
      delay - delay in milliseconds before task is to be executed.
      period - time in milliseconds between successive task executions.
    • scheduleAtFixedRateOnLayoutThread

      public static void scheduleAtFixedRateOnLayoutThread(@Nonnull TimerTask task, @Nonnull Date firstTime, long period)
      Schedules the specified task for repeated fixed-delay execution, on the Layout Thread, beginning at the specified time. Subsequent executions take place at approximately regular intervals, separated by the specified period.
      Parameters:
      task - task to be scheduled.
      firstTime - First time at which task is to be executed.
      period - time in milliseconds between successive task executions.
    • scheduleAtFixedRateOnLayoutThread

      public static void scheduleAtFixedRateOnLayoutThread(@Nonnull TimerTask task, long delay, long period)
      Schedules the specified task for repeated fixed-delay execution on the Layout Thread beginning after the specified delay. Subsequent executions take place at approximately regular intervals separated by the specified period.
      Parameters:
      task - task to be scheduled.
      delay - delay in milliseconds before task is to be executed.
      period - time in milliseconds between successive task executions.