DESY ACOP Beans Home

com.cosylab.util
Class Scheduler

java.lang.Object
  extended by java.lang.Thread
      extended by com.cosylab.util.Scheduler
All Implemented Interfaces:
java.lang.Runnable

public class Scheduler
extends java.lang.Thread

Scheduler implementation that prevents single tasks from locking up entire scheduler. This implementation tries to use minimal number of threads to execute given tasks.

Tasks are scheduled using a priority queue, sorted by next execution time. Time is measured using wait method, so it is bound to be incorrect. This implementation is not suitable for scheduling times below 10ms, but it depends on platform (limits are about 50ms for Windows9x, 10ms for Windows 2000/XP/NT and 1ms for Linux/Unix).
Each task will run only once at any given time (if task is scheduled to be executed every 10ms, but it takes 100ms to run, same task will not be executed 10 times in between).
If a task exceeds its next scheduled execution time while running, next execution time will be assigned by adding SchedulerTask.getInterval() after current task has completed.

Version:
$id$
Author:
Ales Pucelj

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
Scheduler()
          Creates new scheduler and starts it.
Scheduler(int minimumRunners)
          Creates a new scheduler and specifies the minimum number of runner threads alive, regardless the number of tasks.
Scheduler(int min, int max)
           
 
Method Summary
 void cancel()
          Cancels all tasks, runners and stops the scheduler.
protected  void returnRunner(com.cosylab.util.Scheduler.SchedulerRunner runner)
          Returns runner to the pool.
protected  void returnTask(com.cosylab.util.Scheduler.TaskEntry task)
          Puts task back to the queue.
 void run()
          Main loop that executes tasks.
 void schedule(SchedulerTask task)
          Schedule new task for immediate single time execution.
 void schedule(SchedulerTask task, long delay, boolean repeated)
          Schedule task for delayed execution.
 
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
 

Constructor Detail

Scheduler

public Scheduler()
Creates new scheduler and starts it.


Scheduler

public Scheduler(int minimumRunners)
Creates a new scheduler and specifies the minimum number of runner threads alive, regardless the number of tasks.

Parameters:
minimumRunners - Number of threads.

Scheduler

public Scheduler(int min,
                 int max)
Method Detail

returnRunner

protected void returnRunner(com.cosylab.util.Scheduler.SchedulerRunner runner)
Returns runner to the pool. If there are too many runners in the pool, runner will be stopped.

Parameters:
runner - Runner thread to return to pool.

returnTask

protected void returnTask(com.cosylab.util.Scheduler.TaskEntry task)
Puts task back to the queue. If task was marked to be canceled, it will be destroyed.

Parameters:
task - Task to return.

schedule

public void schedule(SchedulerTask task)
Schedule new task for immediate single time execution.

Parameters:
task - Task to schedule.

schedule

public void schedule(SchedulerTask task,
                     long delay,
                     boolean repeated)
Schedule task for delayed execution.

Parameters:
task - Task to schedule
delay - Delay before initial execution.
repeated - Should this task be repeated or executed only once.

run

public void run()
Main loop that executes tasks.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread
See Also:
Runnable.run()

cancel

public void cancel()
Cancels all tasks, runners and stops the scheduler.



Copyright © 2010. All Rights Reserved.