org.webmacro.util
Class TimeLoop

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--org.webmacro.util.TimeLoop
All Implemented Interfaces:
java.lang.Runnable

public class TimeLoop
extends java.lang.Thread

TimeLoop is a scheduler which can schedule Runnable objects for execution or repeate execution. A TimeLoop is configured with two key parameters: periods and duration.

You can schedule a job to execute once, P periods from now, where P must be less than the number of periods in the TimeLoop.

You can also schedule a job to execute once every P periods, where P must be less than the number of periods in the timeloop. TimeLoop will be somewhat inaccurate in this case unless the number of periods in the TimeLoop is a multiple of P: otherwise it cannot distribute the repeat job evenly over its time periods.


Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
TimeLoop(long duration, int periods)
          Create a new TimeLoop.
 
Method Summary
static void main(java.lang.String[] arg)
          Test the TimeLoop
 void run()
          Do not call this method.
 void schedule(java.lang.Runnable task, int waitPeriods)
          Schedule a job to run in the specified number of waitPeriods.
 void scheduleRepeat(java.lang.Runnable task, int waitPeriods)
          Schedule a task to run repeatedly, once every waitPeriods.
 void scheduleRepeatTime(java.lang.Runnable task, long milliseconds)
          A convenience method which translates your milliseconds into wait periods.
 void scheduleTime(java.lang.Runnable task, long milliseconds)
          A convenience method which translates your milliseconds into wait periods.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TimeLoop

public TimeLoop(long duration,
                int periods)
Create a new TimeLoop.
Parameters:
duration - how long is each period? (milliseconds)
periods - how many periods are there in total?
Method Detail

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Thread

run

public void run()
Do not call this method. Call start().
Overrides:
run in class java.lang.Thread

scheduleRepeat

public void scheduleRepeat(java.lang.Runnable task,
                           int waitPeriods)
Schedule a task to run repeatedly, once every waitPeriods. For example, in a 10 period TimeLoop scheduleRepeat(r,2) invoked during period 0 would schedule the job to run in periods 0, 2, 4, 6, and 8. Note that in a 5 period TimeLoop calling scheduleRepeat(r,3) would cause the job to be scheduled only at time period 3.

scheduleRepeatTime

public void scheduleRepeatTime(java.lang.Runnable task,
                               long milliseconds)
A convenience method which translates your milliseconds into wait periods. The milliseconds specify wait duration.

schedule

public void schedule(java.lang.Runnable task,
                     int waitPeriods)
Schedule a job to run in the specified number of waitPeriods. Note that waitPeriods must be less than the total number of periods available in the TimeLoop. For example, if you calld schedule(r,5) at period 7 in a 10 period TimeLoop then r would be scheduled to run in period 2, which will be executed (waitPeriods * duration) milliseconds from now.

scheduleTime

public void scheduleTime(java.lang.Runnable task,
                         long milliseconds)
A convenience method which translates your milliseconds into wait periods. The milliseconds specify wait duration.

main

public static void main(java.lang.String[] arg)
Test the TimeLoop