|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.lang.Thread
dsto.dfc.util.Scheduler
public class Scheduler
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.
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 |
---|
protected ArrayList tasks
protected Scheduler.Task currentTask
protected int workers
protected boolean stopRequested
Constructor Detail |
---|
public Scheduler()
Method Detail |
---|
public int getTaskCount()
public void waitForIdle()
public void schedule(Scheduler.Task task)
schedule(Task, long)
public void schedule(Scheduler.Task task, long delay) throws IllegalArgumentException
task
- The feed.delay
- Delay in millis. Zero or less implies immediate run.
IllegalArgumentException
- If the task is already scheduled. Use
unschedule () first if you want to reschedule,unschedule(Task)
public boolean unschedule(Scheduler.Task task)
task
- The feed.
schedule(Task)
public void shutdown()
public long findLastRunAt()
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.
IllegalStateException
- if there are no tasks scheduled.public void run()
run
in interface Runnable
run
in class Thread
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |