Fixed-delay & fixed-rate single thread scheduler
The scheduler supports varying scheduling intervals by asking the task
every time for its next preferred scheduling interval. Scheduling can
either be
fixed-delay or
fixed-rate. The notions are
borrowed from
java.util.Timer and retain the same meaning.
I.e. in fixed-delay scheduling, the task's new schedule is calculated
as:
new_schedule = time_task_starts + scheduling_interval
In fixed-rate scheduling, the next schedule is calculated as:
new_schedule = time_task_was_supposed_to_start + scheduling_interval
The scheduler internally holds a queue of tasks sorted in ascending order
according to their next execution time. A task is removed from the queue
if it is cancelled, i.e. if
TimeScheduler.Task.isCancelled()
returns true.
The scheduler internally uses a
java.util.SortedSet to keep tasks
sorted.
java.util.Timer uses an array arranged as a binary heap
that doesn't shrink. It is likely that the latter arrangement is faster.
Initially, the scheduler is in
SUSPENDed mode,
start()
need not be called: if a task is added, the scheduler gets started
automatically. Calling
start() starts the scheduler if it's
suspended or stopped else has no effect. Once
stop() is called,
added tasks will not restart it:
start() has to be called to
restart the scheduler.