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 Summary

    Fields
    Modifier and Type
    Field
    Description
    (package private) static final Timer
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    schedule(TimerTask task, long delay)
    Schedules the specified task for execution after the specified delay.
    static void
    schedule(TimerTask task, long delay, long period)
    Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay.
    static void
    schedule(TimerTask task, Date time)
    Schedule a TimerTask for execution at the specified time.
    static void
    schedule(TimerTask task, Date firstTime, long period)
    Schedules the specified task for repeated fixed-delay execution, beginning at the specified time.
    static void
    scheduleAtFixedRate(TimerTask task, long delay, long period)
    Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay.
    static void
    scheduleAtFixedRate(TimerTask task, Date firstTime, long period)
    Schedules the specified task for repeated fixed-delay execution, beginning at the specified time.
    static void
    scheduleAtFixedRateOnGUIThread(TimerTask task, long delay, long period)
    Schedules the specified task for repeated fixed-delay execution on the GUI Thread beginning after the specified delay.
    static void
    scheduleAtFixedRateOnGUIThread(TimerTask task, Date firstTime, long period)
    Schedules the specified task for repeated fixed-delay execution, on the GUI Thread, beginning at the specified time.
    static void
    scheduleAtFixedRateOnLayoutThread(TimerTask task, long delay, long period)
    Schedules the specified task for repeated fixed-delay execution on the Layout Thread beginning after the specified delay.
    static void
    scheduleAtFixedRateOnLayoutThread(TimerTask task, Date firstTime, long period)
    Schedules the specified task for repeated fixed-delay execution, on the Layout Thread, beginning at the specified time.
    static void
    scheduleOnGUIThread(TimerTask task, long delay)
    Schedules the specified task for execution on the GUI Thread after the specified delay.
    static void
    scheduleOnGUIThread(TimerTask task, long delay, long period)
    Schedules the specified task for repeated fixed-delay execution on the GUI Thread, beginning after the specified delay.
    static void
    Schedule a TimerTask on GUI Thread for execution at the specified time.
    static void
    scheduleOnGUIThread(TimerTask task, Date firstTime, long period)
    Schedules the specified task for repeated fixed-delay execution on the GUI Thread, beginning at the specified time.
    static void
    scheduleOnLayoutThread(TimerTask task, long delay)
    Schedules the specified task for execution on the Layout Thread after the specified delay.
    static void
    scheduleOnLayoutThread(TimerTask task, long delay, long period)
    Schedules the specified task for repeated fixed-delay execution on the Layout Thread beginning after the specified delay.
    static void
    Schedule a TimerTask on Layout Thread for execution at the specified time.
    static void
    scheduleOnLayoutThread(TimerTask task, Date firstTime, long period)
    Schedules the specified task for repeated fixed-delay execution on the Layout Thread, beginning at the specified time.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      public static void scheduleOnGUIThread(@Nonnull TimerTask task, @Nonnull Date time)
      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.
    • scheduleOnGUIThread

      public static void 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.
    • scheduleOnGUIThread

      public static void 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.
    • scheduleOnGUIThread

      public static void 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.
    • scheduleAtFixedRateOnGUIThread

      public static void 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.
    • scheduleAtFixedRateOnGUIThread

      public static void 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.
    • 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.