Documentation of 'edu.rit.util.TimerThread' Java class
TimerThread
edu.rit.util

Class TimerThread

  • All Implemented Interfaces:
    java.lang.Runnable


    public class TimerThread
    extends java.lang.Thread
    Class TimerThread encapsulates a thread that does the timing for Timers and performs TimerTasks' actions when timeouts occur.

    A timer is created by calling a timer thread's createTimer() method, giving a timer task to associate with the timer. Multiple timers may be created under the control of the same timer thread. The timer thread will perform the actions of its timers' timer tasks one at a time at the proper instants (by causing each timer to call its timer task's action() method). Note that the timer tasks of a single timer thread are performed sequentially, which may cause some timer tasks' actions to be delayed depending on how long other timer tasks' actions take to execute. If necessary, consider running separate timers in separate timer threads so the timer tasks' actions run concurrently.

    Class TimerThread does not offer real-time guarantees. It merely makes a best-effort attempt to perform each timer task's actions as soon as possible after the timeouts occur.

    A TimerThread is just a Thread. After constructing a timer thread, you can mark it as a daemon thread if you want. You must also call the timer thread's start() method, or no timeouts will occur. To gracefully stop a timer thread, call the shutdown() method.

    To simplify writing programs with multiple objects that all use the same timer thread, the static TimerThread.getDefault() method returns a single shared instance of class TimerThread. The default timer thread is marked as a daemon thread and is started automatically. The default timer thread is not created until the first call to TimerThread.getDefault().

    Classes Timer, TimerTask, and TimerThread provide capabilities similar to classes java.util.Timer and java.util.TimerTask. Unlike the latter, they also provide the ability to stop and restart a timer and the ability to deal with race conditions in multithreaded programs.

    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.lang.Thread

        java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
    • Field Summary

      • Fields inherited from class java.lang.Thread

        MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
    • Constructor Summary

      Constructors 
      Constructor and Description
      TimerThread()
      Construct a new timer thread.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      Timer createTimer(TimerTask theTimerTask)
      Create a new timer associated with the given timer task and under the control of this timer thread.
      static TimerThread getDefault()
      Get the default timer thread, a single shared instance of class TimerThread.
      void run()
      Perform this timer thread's processing.
      void shutdown()
      Shut down this timer thread.
      • Methods inherited from class java.lang.Thread

        activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • TimerThread

        public TimerThread()
        Construct a new timer thread. After constructing it, you must call the timer thread's start() method, or no timeouts will occur.
    • Method Detail

      • getDefault

        public static TimerThread getDefault()
        Get the default timer thread, a single shared instance of class TimerThread. The default timer thread is marked as a daemon thread and is started automatically. The default timer thread is not created until the first call to TimerThread.getDefault().
        Returns:
        Default timer thread.
      • createTimer

        public Timer createTimer(TimerTask theTimerTask)
        Create a new timer associated with the given timer task and under the control of this timer thread. When the timer is triggered, this timer thread will cause the timer to call the given timer task's action() method.
        Parameters:
        theTimerTask - Timer task.
        Throws:
        java.lang.NullPointerException - (unchecked exception) Thrown if theTimerTask is null.
      • shutdown

        public void shutdown()
        Shut down this timer thread.
      • run

        public void run()
        Perform this timer thread's processing. (Never call the run() method yourself!)
        Specified by:
        run in interface java.lang.Runnable
        Overrides:
        run in class java.lang.Thread
        Throws:
        java.lang.IllegalStateException - (unchecked exception) Thrown if some thread other than this timer thread called the run() method.

DMelt 3.0 © DataMelt by jWork.ORG

You see the box below because you did not login.