Class Timer


  • public final class Timer
    extends Object
    Allows scheduling tasks (Runnables) to be executed on an Executor after some delay.

    Starts one thread that processes submitted tasks in a loop and when it's time for a task to run, it gets submitted to the executor.

    • Constructor Detail

      • Timer

        public Timer​(Executor executor)
        Parameters:
        executor - default Executor used for running scheduled tasks, unless an executor is provided when scheduling a task
    • Method Detail

      • schedule

        public TimerTask schedule​(long delayInMillis,
                                  Runnable task)
        Schedules the task to be executed in delayInMillis on the Executor specified when creating this Timer.

        Equivalent to schedule(delayInMillis, task, null).

      • schedule

        public TimerTask schedule​(long delayInMillis,
                                  Runnable task,
                                  Executor executor)
        Schedules the task to be executed in delayInMillis on given executor. If executor is null, the Executor specified when creating this Timer is used.
      • shutdown

        public void shutdown()
                      throws InterruptedException
        Should be called before the underlying executor is shut down. Returns only after the timer thread finishes.
        Throws:
        InterruptedException