org.jgroups.util
Class TimeScheduler

java.lang.Object
  extended by java.util.Timer
      extended by org.jgroups.util.TimeScheduler

public class TimeScheduler
extends java.util.Timer

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.

Version:
$Id: TimeScheduler.java,v 1.14.2.2 2007/04/27 09:11:18 belaban Exp $
Author:
Bela Ban

Nested Class Summary
static interface TimeScheduler.CancellableTask
           
static interface TimeScheduler.Task
          The interface that submitted tasks must implement
 
Field Summary
protected static org.apache.commons.logging.Log log
           
 
Constructor Summary
TimeScheduler()
           
TimeScheduler(boolean isDaemon)
           
 
Method Summary
 void add(TimeScheduler.Task t)
          Add a task for execution at adjustable intervals
 void add(TimeScheduler.Task task, boolean relative)
          Add a task for execution at adjustable intervals
 void cancel()
           
 java.lang.String dumpTaskQueue()
           
 void schedule(java.util.TimerTask task, java.util.Date time)
           
 void schedule(java.util.TimerTask task, java.util.Date firstTime, long period)
           
 void schedule(java.util.TimerTask task, long delay)
           
 void schedule(java.util.TimerTask task, long delay, long period)
           
 void scheduleAtFixedRate(java.util.TimerTask task, java.util.Date firstTime, long period)
           
 void scheduleAtFixedRate(java.util.TimerTask task, long delay, long period)
           
 int size()
          Returns the number of tasks currently scheduled.
 void start()
          Start the scheduler, if it's suspended or stopped
 void stop()
          Stop the scheduler if it's running.
 
Methods inherited from class java.util.Timer
purge
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected static final org.apache.commons.logging.Log log
Constructor Detail

TimeScheduler

public TimeScheduler()

TimeScheduler

public TimeScheduler(boolean isDaemon)
Method Detail

dumpTaskQueue

public java.lang.String dumpTaskQueue()

add

public void add(TimeScheduler.Task task,
                boolean relative)
Add a task for execution at adjustable intervals

Parameters:
task - the task to execute
relative - scheduling scheme:

true:
Task is rescheduled relative to the last time it actually started execution

false:
Task is scheduled relative to its last execution schedule. This has the effect that the time between two consecutive executions of the task remains the same.

April 07: the relative argument is ignored, will always be true


add

public void add(TimeScheduler.Task t)
Add a task for execution at adjustable intervals

Parameters:
t - the task to execute

schedule

public void schedule(java.util.TimerTask task,
                     long delay)
Overrides:
schedule in class java.util.Timer

schedule

public void schedule(java.util.TimerTask task,
                     long delay,
                     long period)
Overrides:
schedule in class java.util.Timer

schedule

public void schedule(java.util.TimerTask task,
                     java.util.Date firstTime,
                     long period)
Overrides:
schedule in class java.util.Timer

schedule

public void schedule(java.util.TimerTask task,
                     java.util.Date time)
Overrides:
schedule in class java.util.Timer

scheduleAtFixedRate

public void scheduleAtFixedRate(java.util.TimerTask task,
                                long delay,
                                long period)
Overrides:
scheduleAtFixedRate in class java.util.Timer

scheduleAtFixedRate

public void scheduleAtFixedRate(java.util.TimerTask task,
                                java.util.Date firstTime,
                                long period)
Overrides:
scheduleAtFixedRate in class java.util.Timer

cancel

public void cancel()
Overrides:
cancel in class java.util.Timer

size

public int size()
Returns the number of tasks currently scheduled. Note that this is an approximation.

Returns:
The number of tasks currently in the queue.

start

public void start()
Start the scheduler, if it's suspended or stopped


stop

public void stop()
          throws java.lang.InterruptedException
Stop the scheduler if it's running. Switch to stopped, if it's suspended. Clear the task queue.

Throws:
java.lang.InterruptedException - if interrupted while waiting for thread to return


Copyright ? 1998-2006 Bela Ban. All Rights Reserved.