Class TimerTask

  • All Implemented Interfaces:
    java.lang.Comparable, Executable

    public abstract class TimerTask
    extends java.lang.Object
    implements Executable, java.lang.Comparable
    A class that represent a task that can be scheduled for one-shot or repeated execution by a TimerQueue.

    A similar class is present in java.util package of jdk version >= 1.3; for compatibility with jdk 1.2 we reimplemented it.

    Version:
    $Revision$
    See Also:
    TimerQueue
    • Field Summary

      Fields 
      Modifier and Type Field Description
      (package private) static int CANCELLED
      The state when cancelled
      (package private) static int EXECUTED
      The state after first execution if the TimerTask is not repeating
      private java.lang.Object m_lock  
      private long m_nextExecutionTime  
      private long m_period  
      private int m_state  
      (package private) static int NEW
      The state before the first execution
      (package private) static int SCHEDULED
      The state after first execution if the TimerTask is repeating
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected TimerTask()
      Creates a TimerTask object that will be executed once.
      protected TimerTask​(long period)
      Creates a TimerTask object that will be executed every period milliseconds.
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      boolean cancel()
      Cancels the next execution of this TimerTask (if any).
      int compareTo​(java.lang.Object other)
      A TimerTask is less than another if it will be scheduled before.
      abstract void execute()
      The task to be executed, to be implemented in subclasses.
      (package private) java.lang.Object getLock()
      Returns the mutex that syncs the access to this object
      (package private) long getNextExecutionTime()
      Returns the next execution time for this TimerTask
      protected long getPeriod()  
      (package private) int getState()
      Returns the state of execution of this TimerTask
      (package private) boolean isPeriodic()
      Returns whether this TimerTask is periodic
      (package private) void setNextExecutionTime​(long time)
      Sets the next execution time for this TimerTask
      (package private) void setState​(int state)
      Sets the state of execution of this TimerTask
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • SCHEDULED

        static final int SCHEDULED
        The state after first execution if the TimerTask is repeating
        See Also:
        Constant Field Values
      • EXECUTED

        static final int EXECUTED
        The state after first execution if the TimerTask is not repeating
        See Also:
        Constant Field Values
      • m_lock

        private final java.lang.Object m_lock
      • m_state

        private int m_state
      • m_period

        private final long m_period
      • m_nextExecutionTime

        private long m_nextExecutionTime
    • Constructor Detail

      • TimerTask

        protected TimerTask()
        Creates a TimerTask object that will be executed once.
      • TimerTask

        protected TimerTask​(long period)
        Creates a TimerTask object that will be executed every period milliseconds.

        Parameters:
        period - the execution period; if zero, will be executed only once.
    • Method Detail

      • cancel

        public boolean cancel()
        Cancels the next execution of this TimerTask (if any).
        If the TimerTask is executing it will prevent the next execution (if any).
        Returns:
        true if one or more scheduled execution will not take place, false otherwise.
      • execute

        public abstract void execute()
                              throws java.lang.Exception
        The task to be executed, to be implemented in subclasses.
        Specified by:
        execute in interface Executable
        Throws:
        java.lang.Exception
      • compareTo

        public int compareTo​(java.lang.Object other)
        A TimerTask is less than another if it will be scheduled before.
        Specified by:
        compareTo in interface java.lang.Comparable
        Throws:
        java.lang.ClassCastException - if other is not a TimerTask, according to the Comparable contract
      • getLock

        java.lang.Object getLock()
        Returns the mutex that syncs the access to this object
      • setState

        void setState​(int state)
        Sets the state of execution of this TimerTask
      • getState

        int getState()
        Returns the state of execution of this TimerTask
      • isPeriodic

        boolean isPeriodic()
        Returns whether this TimerTask is periodic
      • getNextExecutionTime

        long getNextExecutionTime()
        Returns the next execution time for this TimerTask
      • setNextExecutionTime

        void setNextExecutionTime​(long time)
        Sets the next execution time for this TimerTask
      • getPeriod

        protected long getPeriod()
        Returns:
        the period of this TimerTask