com.partnersoft.gadgets
Class LoopingThread

java.lang.Object
  extended by java.lang.Thread
      extended by com.partnersoft.gadgets.LoopingThread
All Implemented Interfaces:
java.lang.Runnable

public class LoopingThread
extends java.lang.Thread

Takes care of the (surprisingly annoying) case of repeating a task at a regular interval. Handles the problems of stopping and starting the process, and of timing the sleeps so that it iterates at the specified interval (rather than sleeping a constant amount between runs). To use, either instantiate it with a Runnable object with a run() method that does what you want to do each time we loop, or alternately use an anonymous or subclass and override the runTask() method. Do not override run() on the LoopingThread; that method is what provides our looping behavior!

Author:
Paul Reavis Copyright 2004-2005 Partner Software, Inc.

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
LoopingThread()
           
LoopingThread(java.lang.String name)
           
LoopingThread(java.lang.String name, int interval)
           
LoopingThread(java.lang.String name, int interval, java.lang.Runnable task)
           
LoopingThread(java.lang.String name, java.lang.Runnable task)
           
 
Method Summary
 int getInterval()
          Gets the loop interval in milliseconds.
 java.lang.Runnable getTask()
           
 void pauseLooping()
           
 void resumeLooping()
           
 void run()
           
 void runTask()
          By default, this runs the runnable.
 void setInterval(int newInterval)
          Sets the loop interval in milliseconds.
 void setTask(java.lang.Runnable newTask)
           
 void stopRunning()
          Tells the thread to quit looping.
 
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
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LoopingThread

public LoopingThread()

LoopingThread

public LoopingThread(java.lang.String name)

LoopingThread

public LoopingThread(java.lang.String name,
                     java.lang.Runnable task)

LoopingThread

public LoopingThread(java.lang.String name,
                     int interval)

LoopingThread

public LoopingThread(java.lang.String name,
                     int interval,
                     java.lang.Runnable task)
Method Detail

pauseLooping

public void pauseLooping()

resumeLooping

public void resumeLooping()

run

public void run()
Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

runTask

public void runTask()
             throws java.lang.Exception
By default, this runs the runnable. Subclasses may also override it directly (e.g. in anonymous or inner classes). Exceptions will be caught and reported but will stop the thread.

Throws:
java.lang.Exception

stopRunning

public void stopRunning()
Tells the thread to quit looping. Waits for it to complete.


setInterval

public void setInterval(int newInterval)
Sets the loop interval in milliseconds.


getInterval

public int getInterval()
Gets the loop interval in milliseconds.


setTask

public void setTask(java.lang.Runnable newTask)

getTask

public java.lang.Runnable getTask()