dsto.dfc.util
Class Scheduler

java.lang.Object
  extended by java.lang.Thread
      extended by dsto.dfc.util.Scheduler
All Implemented Interfaces:
Runnable

public class Scheduler
extends Thread

A lightweight task scheduler thread similar to java.util.Timer. The reason for this class' existence is that the Java Timer class does not allow Tasks to be rescheduled, meaning you can't create a task, schedule it and then reschedule for some later time.

Threading implementation note: while a task is executing, the scheduler thread owns its monitor, but not the scheduler monitor so the task is not forced to block access to the scheduler by other threads. So, to avoid the possibility of deadlock if the task does decide to acquire the scheduler monitor (most commonly by calling schedule(Task) to reschedule itself), when the scheduler needs both its own monitor and a task's, it always acquires the task's first, then its own.

Author:
Matthew Phillips

Nested Class Summary
static class Scheduler.Task
          Base class for tasks that can be scheduled.
 
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
 
Field Summary
protected  Scheduler.Task currentTask
           
protected  boolean stopRequested
           
protected  ArrayList tasks
           
protected  int workers
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
Scheduler()
           
 
Method Summary
 long findLastRunAt()
          Returns the time at which the last scheduled task will be run in milliseconds since midnight, January 1, 1970, UTC.
 int getTaskCount()
           
 void run()
           
 void schedule(Scheduler.Task task)
          Schedule a task to run immediately.
 void schedule(Scheduler.Task task, long delay)
          Schedule a task to run with a given delay.
 void shutdown()
          Shutdown at the next opportunity.
 boolean unschedule(Scheduler.Task task)
          Unschedule a task.
 void waitForIdle()
          Waits, via a 100 millisecond polling loop, for this scheduler to became idle, that is, the scheduler has no more tasks pending run and that it is not currently running a task.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

tasks

protected ArrayList tasks

currentTask

protected Scheduler.Task currentTask

workers

protected int workers

stopRequested

protected boolean stopRequested
Constructor Detail

Scheduler

public Scheduler()
Method Detail

getTaskCount

public int getTaskCount()

waitForIdle

public void waitForIdle()
Waits, via a 100 millisecond polling loop, for this scheduler to became idle, that is, the scheduler has no more tasks pending run and that it is not currently running a task.


schedule

public void schedule(Scheduler.Task task)
Schedule a task to run immediately.

See Also:
schedule(Task, long)

schedule

public void schedule(Scheduler.Task task,
                     long delay)
              throws IllegalArgumentException
Schedule a task to run with a given delay. If the task is currently running, this blocks until the task finishes.

Parameters:
task - The feed.
delay - Delay in millis. Zero or less implies immediate run.
Throws:
IllegalArgumentException - If the task is already scheduled. Use unschedule () first if you want to reschedule,
See Also:
unschedule(Task)

unschedule

public boolean unschedule(Scheduler.Task task)
Unschedule a task. If the task is running, this will block until it finishes. This will do nothing if the task is not scheduled.

Parameters:
task - The feed.
Returns:
True if at least one instance was unscheduled.
See Also:
schedule(Task)

shutdown

public void shutdown()
Shutdown at the next opportunity. Blocks until any currently running tasks have exited and disposes any not already running.


findLastRunAt

public long findLastRunAt()
Returns the time at which the last scheduled task will be run in milliseconds since midnight, January 1, 1970, UTC. Used to schedule tasks a period of time after the last known task will be run.

NB Use getTaskCount() to check if any tasks are currently scheduled. If no tasks are scheduled this method will throw an IllegalStateException because it doesn't make sense to get the run time of a non-existent task.

Returns:
The time at which the last known task is scheduled to run.
Throws:
IllegalStateException - if there are no tasks scheduled.

run

public void run()
Specified by:
run in interface Runnable
Overrides:
run in class Thread


Copyright © 2008 Commonwealth of Australia