Package java.util

Class Timer


  • public class Timer
    extends Object
    Class to schedule tasks in a separate thread.

    Each timer is associated with precisely one “execution thread”, which it uses to run TimerTask instances serially. Scheduled TimerTask instances may either be one-shot or recurring. Recurring tasks can be scheduled as fixed-period with schedule(java.util.TimerTask, java.util.Date) or as fixed-rate with scheduleAtFixedRate(java.util.TimerTask, long, long).

    Timers are thread safe in the sense that several threads can operate on one instance at the same time.

    • Constructor Summary

      Constructors 
      Constructor Description
      Timer()
      Equivalent to this(false)
      Timer​(boolean isDaemon)
      Creates a new timer with a default (unique) name.
      Timer​(String name)
      Equivalent to this(name, false).
      Timer​(String name, boolean isDaemon)
      Create a new timer with name name.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void cancel()
      Cancels all remaining tasks on the timer.
      protected void finalize()
      Make sure the thread is stopped when the timer is garbage-collected.
      int purge()
      Removes all references held by this instance to cancelled timer-tasks.
      void schedule​(TimerTask task, long delay)
      Schedules a one-shot task after a fixed delay delay, specified in milliseconds.
      void schedule​(TimerTask task, long delay, long period)
      Schedules a recurring fixed-period task after a fixed delay delay with period period.
      void schedule​(TimerTask task, Date when)
      Schedules a one-shot task at a fixed time when.
      void schedule​(TimerTask task, Date when, long period)
      Schedules a recurring fixed-period task after a fixed time when with period period.
      void scheduleAtFixedRate​(TimerTask task, long delay, long period)
      Schedules a recurring fixed-rate task after a fixed delay delay with period period.
      void scheduleAtFixedRate​(TimerTask task, Date when, long period)
      Schedules a recurring fixed-rate task after a fixed time when with period period.
    • Constructor Detail

      • Timer

        public Timer​(String name,
                     boolean isDaemon)
        Create a new timer with name name. isDaemon specifies whether the execution thread is a daemon or not.
      • Timer

        public Timer​(String name)
        Equivalent to this(name, false).
      • Timer

        public Timer​(boolean isDaemon)
        Creates a new timer with a default (unique) name.
      • Timer

        public Timer()
        Equivalent to this(false)
    • Method Detail

      • finalize

        protected void finalize()
                         throws Throwable
        Make sure the thread is stopped when the timer is garbage-collected.
        Overrides:
        finalize in class Object
        Throws:
        Throwable
      • cancel

        public void cancel()
        Cancels all remaining tasks on the timer.

        The task that’s currently running (if any) must be allowed to complete. No further tasks can be enqueued on this timer. Also, releases all resources associated with this timer. All subsequent calls to methods on this instance must be no-ops.

      • purge

        public int purge()
        Removes all references held by this instance to cancelled timer-tasks.

        Those tasks will be collectible at the end of this method if no other references to them exist. Returns the number of tasks purged.

      • schedule

        public void schedule​(TimerTask task,
                             Date when)
        Schedules a one-shot task at a fixed time when.
      • schedule

        public void schedule​(TimerTask task,
                             long delay)
        Schedules a one-shot task after a fixed delay delay, specified in milliseconds.
      • schedule

        public void schedule​(TimerTask task,
                             long delay,
                             long period)
        Schedules a recurring fixed-period task after a fixed delay delay with period period.

        Both delay and period are specified in milliseconds.

      • schedule

        public void schedule​(TimerTask task,
                             Date when,
                             long period)
        Schedules a recurring fixed-period task after a fixed time when with period period.

        Period is specified in milliseconds.

      • scheduleAtFixedRate

        public void scheduleAtFixedRate​(TimerTask task,
                                        long delay,
                                        long period)
        Schedules a recurring fixed-rate task after a fixed delay delay with period period.

        Both delay and period are specified in milliseconds.

      • scheduleAtFixedRate

        public void scheduleAtFixedRate​(TimerTask task,
                                        Date when,
                                        long period)
        Schedules a recurring fixed-rate task after a fixed time when with period period.

        Period is specified in milliseconds.