Class ThreadTimer

  • All Implemented Interfaces:
    Timer

    public final class ThreadTimer
    extends Object
    implements Timer
    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. The default executor is provided by a caller, so the caller must shut down this timer before shutting down the executor.

    At most one timer may exist.

    • Method Detail

      • create

        public static ThreadTimer create​(Executor defaultExecutor)
        Creates a timer with given defaultExecutor, unless a timer already exists, in which case an exception is thrown.
        Parameters:
        defaultExecutor - default Executor used for running scheduled tasks, unless an executor is provided when scheduling a task
      • schedule

        public TimerTask schedule​(long delayInMillis,
                                  Runnable task)
        Description copied from interface: Timer
        Schedules the task to be executed in delayInMillis on this timer's default Executor.

        Equivalent to schedule(delayInMillis, task, null).

        Specified by:
        schedule in interface Timer
      • schedule

        public TimerTask schedule​(long delayInMillis,
                                  Runnable task,
                                  Executor executor)
        Description copied from interface: Timer
        Schedules the task to be executed in delayInMillis on given executor. If executor is null, this timer's default executor is used.
        Specified by:
        schedule in interface Timer
      • countScheduledTasks

        public int countScheduledTasks()
        Description copied from interface: Timer
        Returns the number of tasks that are currently scheduled for execution by the timer. Finished tasks and tasks that are already running are not included.
        Specified by:
        countScheduledTasks in interface Timer
        Returns:
        the number of currently scheduled tasks