com.ccg.util
Class Scheduler

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

public class Scheduler
extends Thread

Class to do something periodically.

Java supports threads very nicely. However, there are times when threads are overkill. For example, if you find yourself wanting to check something every few seconds, you would typically start a thread that would run in the background. It would do its check, then sleep for a period of time and continue on. For each one of these types of checks, you typically create a separate running thread, leaving your system with a lot of threads which are typically in a sleep state.

This class attempts to reduce these types of situations to a single thread. It works in the following manner:

This results in one thread capable of doing a lot of "periodic" things.

IMPORTANT NOTE: You can really "muck-up" the Scheduler by adding a Schedulable item that does not exit from its run() method.

A sample program SchedTest.java has been provided to demonstrate how one can use this class to cause periodic actions to occur.

Since:
1.0
Version:
$Revision: 1.13 $
Author:
$Author: pkb $
See Also:
addToDefault(com.ccg.util.Schedulable), Schedulable, ScheduleEntry, MemMonitor

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
 
Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
Scheduler()
          Default constructor for the Scheduler.
 
Method Summary
 void add(Schedulable s)
          Add a new Schedulable object to the Scheduler This method takes a new Schedulable item and inserts it into the schedule list such that the item's run method will be invoked after the amount of time specified by its delay time has been reached.
static void addToDefault(Schedulable s)
          Add a new Schedulable item to the default system Scheduler.
 void clear()
          Stops the scheduler thread AND clear all of the scheduled entries.
 void remove(Schedulable s)
          Remove a Schedulable object from the Scheduler.
static void removeFromDefault(Schedulable s)
          Remove a Schedulable item from the default system Scheduler.
 void run()
          The main loop of the Scheduler object.
 void shutdown()
          Nicely stops the Scheduler.
 
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()
Default constructor for the Scheduler. This default constructor initializes the Scheduler and prepares it such that it can be run in the foreground, started as a background thread, and/or have additional Schedulable items added to it.

Since:
1.0
See Also:
add(com.ccg.util.Schedulable), run()
Method Detail

addToDefault

public static void addToDefault(Schedulable s)
Add a new Schedulable item to the default system Scheduler. This static method does the following:

Since most users of this class will want all of their Schedulable objects to be run by the same Scheduler, this is the easiest way to accomplish it. You simply keep calling this static method with your new Schedulable objects which you want to have run periodically - everything else is automatic.

Parameters:
s - New Schedulable item to insert into the schedule list.
Since:
1.0
See Also:
add(com.ccg.util.Schedulable)

removeFromDefault

public static void removeFromDefault(Schedulable s)
Remove a Schedulable item from the default system Scheduler. This static method is used to remove your schedulable item from the system scheduler.

Parameters:
s - Schedulable item to remove from the schedule list.
Since:
1.0
See Also:
addToDefault(com.ccg.util.Schedulable)

add

public void add(Schedulable s)
Add a new Schedulable object to the Scheduler This method takes a new Schedulable item and inserts it into the schedule list such that the item's run method will be invoked after the amount of time specified by its delay time has been reached.

Note: The Scheduler depends on all of its Schedulable items behaving properly. If a Schedulable's run() method blocks or loops indefinitely, it will prevent the Scheduler from running any of its other Schedulable items.

After each invocation, the Schedulable items delay time will be fetched. If the delay time returns a value greater than 0, then the Schedulable item will be re-inserted into the schedule table according to the delay time. For example, if the Schedulable.getDelayMillis() always returns 5000 for the Schedulable item, then the Schedulable item will be run every 5 seconds.

Parameters:
s - New Schedulable item to insert into the schedule list.
Since:
1.0
See Also:
run(), ScheduleEntry

remove

public void remove(Schedulable s)
Remove a Schedulable object from the Scheduler.

This method allows one to remove a schedulable item from the scheduler.

Parameters:
s - The Schedulable entry which was previously added that you would like to remove.
Since:
1.0
See Also:
add(com.ccg.util.Schedulable)

run

public void run()
The main loop of the Scheduler object. This is the main loop of the Scheduler object. It runs continually and invokes the run() method of each of its contained Schedulable items as necessary. Typically you do not invoke this method directly. Typically you will use the start() method to run the Scheduler as a background thread.

Specified by:
run in interface Runnable
Overrides:
run in class Thread
Since:
1.0
See Also:
add(com.ccg.util.Schedulable)

clear

public void clear()
Stops the scheduler thread AND clear all of the scheduled entries.

Since:
1.0
See Also:
shutdown()

shutdown

public void shutdown()
Nicely stops the Scheduler. This method trys to nicely stop the Scheduler. It sets an internal flag indicating that the Scheduler should stop and then interrupts the Scheduler hoping it will detect the flag and shut down.

Since:
1.0
See Also:
run()


Copyright 1998-1998-2006 null. All Rights Reserved.