|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.lang.Thread
com.ccg.util.Scheduler
public class Scheduler
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:
Schedulable object (typically derived from
the ScheduleEntry class.
Schedulable object created, you add them to a Scheduler, or add them
to the system wide (shared) Scheduler.
Scheduler (unless you used the
system wide default Scheduler - which is
automatically started when first used).
Scheduler class takes
care of invoking the run() method of each Schedulable item
added.
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.
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 |
|---|
public Scheduler()
run in the foreground,
started as a background thread, and/or have additional Schedulable items added to it.
add(com.ccg.util.Schedulable),
run()| Method Detail |
|---|
public static void addToDefault(Schedulable s)
Schedulable item to the default system Scheduler.
This static method does the following:
Scheduler object
(if it hasn't been created yet).
Schedulable item to the default
system Scheduler.
Scheduler was created by
this invocation, it will be started as a background thread.
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.
s - New Schedulable item to insert into the schedule list.add(com.ccg.util.Schedulable)public static void removeFromDefault(Schedulable s)
Schedulable item from the default system Scheduler.
This static method is used to remove your schedulable item from
the system scheduler.
s - Schedulable item to remove from the schedule list.addToDefault(com.ccg.util.Schedulable)public void add(Schedulable s)
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.
s - New Schedulable item to insert into the schedule list.run(),
ScheduleEntrypublic void remove(Schedulable s)
Schedulable object from the Scheduler.
This method allows one to remove a schedulable item from the scheduler.
s - The Schedulable entry which was previously added that
you would like to remove.add(com.ccg.util.Schedulable)public void run()
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.
run in interface Runnablerun in class Threadadd(com.ccg.util.Schedulable)public void clear()
shutdown()public void shutdown()
run()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||