gnu.cajo.utils.extra
Class Timer

java.lang.Object
  extended by gnu.cajo.utils.extra.Timer
All Implemented Interfaces:
java.io.Serializable

public final class Timer
extends java.lang.Object
implements java.io.Serializable

This class supports the timed execution of scheduled tasks. The class makes use of the Scheduler class, in accomplishing its functionality. This means that shared objects between regular Scheduled tasks, and these timed tasks, will not run into concurrency issues. The object accepts tasks which implement the void slice() method, using the semantics defined in the Scheduler class. Additionally an interval is provided, in milliseconds, for approximate inter-execution delay. A count can also be provided, to allow the task to be run a fixed number of times. When a count of zero is provided, the task will run forever, or until removed. Delay intervals are generally expected to be several seconds, or longer. Delays much less than one second, may not be scheduled accurately.

The class methods are properly synchronized to safely allow multi-thread, and remote access.

Note: as with the Scheduler class, this class also supports serialisation. Therefore, in order for the serialisation to succeed, each of the loaded tasks must also be serialisable.

Version:
1.0, 16-Dec-97 Initial release
Author:
John Catherino
See Also:
Serialized Form

Constructor Summary
Timer(Scheduler sched)
          The constructor loads the timer into the Scheduler, but does not yet start it running.
 
Method Summary
 void load(java.lang.Object task, long interval, int count)
          This method loads a task for timed execution.
 void remove(java.lang.Object task)
          This method prematurely removes a task from the timed execution queue.
 void slice()
          The timer task implementation itself, invoked by the scheduler.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Timer

public Timer(Scheduler sched)
The constructor loads the timer into the Scheduler, but does not yet start it running. It will start itself automatically, when a task is loaded, and will stop itself similarly, when it has no tasks to run.

Parameters:
sched - The scheduler to use for execution.
Method Detail

load

public void load(java.lang.Object task,
                 long interval,
                 int count)
This method loads a task for timed execution. If the timer object is not currently running in its Scheduler, it will be started automatically. Naturally, the task object must implement a public void slice() method. Note: the task will delay a full interval before its first execution. If an immediate execution of the is required, it must be by the caller.

Parameters:
task - The operation to be timed, it can be local, remote, or even a proxy, when enabled.
interval - The time between execution, in milliseconds
count - The number of times to run before removal, a count of zero indicates to run indefinitely, or until removed

remove

public void remove(java.lang.Object task)
This method prematurely removes a task from the timed execution queue. If the timed task queue becomes empty as a result, the timer task will be stopped in its Scheduler object. If multiple instances of the same task have been loaded, the first encountered in the queue will be removed.

Parameters:
task - The task to discard

slice

public void slice()
The timer task implementation itself, invoked by the scheduler. This method will scan the scheduled tasks for timeout, indicating readiness for execution. Additionally it will decrement the task's count, when applicable, and remove the task when the count reaches zero. When the task list becomes empty, it will suspend itself as a scheduled task, until a new task is loaded. If several tasks are eligible for execution in any given slice, only one eligible task will be run, to improve scheduler responsiveness. If the execution of the task slice results in an exception, the task will be automatically dropped from the queue.