org.webmacro.util
Class TimeLoop

java.lang.Object
  extended byorg.webmacro.util.TimeLoop

public class TimeLoop
extends java.lang.Object

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.

TimeLoop is running a background thread to schedule its jobs. Instances of this thread are shared by different timeloop objects, that have the same duration and periods settings. If you do not use the timeloop any longer, you should call its destroy method. This will count references to the background thread transparently and will terminate this thread, if it is not used any more.


Constructor Summary
TimeLoop(long duration, int periods)
          Create an instance of a TimeLoop scheduler.
 
Method Summary
 void destroy()
          Destroy this scheduler.
 void finalize()
          Calls destroy to terminate the background thread if no longer needed.
 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.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TimeLoop

public TimeLoop(long duration,
                int periods)
Create an instance of a TimeLoop scheduler. All clients using the same duration and periods settings will be served by the same background thread. Be sure to call the destroy method when you no longer need this scheduler.

Parameters:
duration - duration of each period (in ms)
periods - number of periods for the scheduler.
Method Detail

destroy

public void destroy()
Destroy this scheduler. If no other scheduler use the shared background thread, the thread will be terminated.


finalize

public void finalize()
Calls destroy to terminate the background thread if no longer needed. You should not rely on this method, but call destroy yourself when you no longer need this scheduler.


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.


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.


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.