| java.lang.Object org.hsqldb.lib.HsqlTimer
HsqlTimer | final public class HsqlTimer implements ObjectComparator,ThreadFactory(Code) | | Facility to schedule tasks for future execution in a background thread.
Tasks may be scheduled for one-time execution or for repeated execution at
regular intervals, using either fixed rate or fixed delay policy.
This class is a JDK 1.1 compatible implementation required by HSQLDB both
because the java.util.Timer class is available only in JDK 1.3+ and because
java.util.Timer starves least recently added tasks under high load and
fixed rate scheduling, especially when the average actual task duration is
greater than the average requested task periodicity.
An additional (minor) advantage over java.util.Timer is that this class does
not retain a live background thread during periods when the task queue is
empty.
author: boucherb@users version: 1.8.0.3 since: 1.7.2 |
Inner Class :protected class TaskRunner implements Runnable | |
Inner Class :protected class Task | |
Inner Class :protected static class TaskQueue extends HsqlArrayHeap | |
Field Summary | |
protected volatile boolean | isShutdown Whether this timer should disallow all further processing. | static int | nowCount | final protected TaskQueue | taskQueue The priority queue for the scheduled tasks. | final protected TaskRunner | taskRunner The inner runnable that executes tasks in the background thread. | protected Thread | taskRunnerThread The background thread. | final protected ThreadFactory | threadFactory The factory that procduces the background threads. |
Constructor Summary | |
public | HsqlTimer() Constructs a new HsqlTimer using the default thread factory
implementation. | public | HsqlTimer(ThreadFactory threadFactory) Constructs a new HsqlTimer.
Uses the specified thread factory implementation.
Parameters: threadFactory - the ThreadFactory used to produce this timer'sbackground threads. |
Method Summary | |
protected Task | addTask(long first, Runnable runnable, long period, boolean relative) Adds to the task queue a new Task object encapsulating the supplied
Runnable and scheduling arguments. | public static void | cancel(Object task) Causes the task referenced by the supplied argument to be cancelled. | protected synchronized void | clearThread() Sets the background thread to null. | public int | compare(Object a, Object b) Required to back the priority queue for scheduled tasks. | public static Date | getLastScheduled(Object task) Retrieves the last time the referenced task was executed, as a
Date object. | public static Date | getNextScheduled(Object task) Retrieves the next time the referenced task is due to be executed, as a
Date object. | public synchronized Thread | getThread() Retrieves the background execution thread. | public static boolean | isCancelled(Object task) Retrieves whether the specified argument references a cancelled task. | public static boolean | isFixedDelay(Object task) Retrieves whether the specified argument references a task scheduled
periodically using fixed delay scheduling. | public static boolean | isFixedRate(Object task) Retrieves whether the specified argument references a task scheduled
periodically using fixed rate scheduling. | public static boolean | isPeriodic(Object task) Retrieves whether the specified argument references a task scheduled
for periodic execution. | public Thread | newThread(Runnable runnable) Default ThreadFactory implementation. | protected Task | nextTask() Retrieves the next task to execute, or null if this timer is shutdown,
the current thread is interrupted, or there are no queued tasks. | public synchronized void | restart() (Re)starts background processing of the task queue. | public Object | scheduleAfter(long delay, Runnable runnable) Causes the specified Runnable to be executed once in the background
after the specified delay.
Parameters: delay - in milliseconds Parameters: runnable - the Runnable to execute. | public Object | scheduleAt(Date date, Runnable runnable) Causes the specified Runnable to be executed once in the background
at the specified time.
Parameters: date - time at which to execute the specified Runnable Parameters: runnable - the Runnable to execute. | public Object | schedulePeriodicallyAfter(long delay, long period, Runnable runnable, boolean relative) Causes the specified Runnable to be executed periodically in the
background, starting after the specified delay. | public Object | schedulePeriodicallyAt(Date date, long period, Runnable runnable, boolean relative) Causes the specified Runnable to be executed periodically in the
background, starting at the specified time. | public static Object | setPeriod(Object task, long period) Sets the periodicity of the designated task to a new value. | public synchronized void | shutdown() Shuts down this timer after the current task (if any) completes. | public synchronized void | shutdownImmediately() Shuts down this timer immediately, interrupting the wait state associated
with the current head of the task queue or the wait state internal to
the currently executing task, if any such state is currently in effect.
After this call, the timer has permanently entered the shutdown state;
attempting to schedule any new task or directly restart this timer will
result in an IllegalStateException. |
isShutdown | protected volatile boolean isShutdown(Code) | | Whether this timer should disallow all further processing.
Once set true, stays true forever.
|
nowCount | static int nowCount(Code) | | stats var
|
taskQueue | final protected TaskQueue taskQueue(Code) | | The priority queue for the scheduled tasks.
|
taskRunner | final protected TaskRunner taskRunner(Code) | | The inner runnable that executes tasks in the background thread.
|
taskRunnerThread | protected Thread taskRunnerThread(Code) | | The background thread.
|
threadFactory | final protected ThreadFactory threadFactory(Code) | | The factory that procduces the background threads.
|
HsqlTimer | public HsqlTimer()(Code) | | Constructs a new HsqlTimer using the default thread factory
implementation.
|
HsqlTimer | public HsqlTimer(ThreadFactory threadFactory)(Code) | | Constructs a new HsqlTimer.
Uses the specified thread factory implementation.
Parameters: threadFactory - the ThreadFactory used to produce this timer'sbackground threads. If null, the default implementation suppliedby this class will be used. |
addTask | protected Task addTask(long first, Runnable runnable, long period, boolean relative)(Code) | | Adds to the task queue a new Task object encapsulating the supplied
Runnable and scheduling arguments.
Parameters: first - the time of the task's first execution Parameters: runnable - the Runnable to execute Parameters: period - the task's periodicity Parameters: relative - if true, use fixed rate else use fixed delay scheduling an opaque reference to the internal task |
cancel | public static void cancel(Object task)(Code) | | Causes the task referenced by the supplied argument to be cancelled.
If the referenced task is currently executing, it will continue until
finished but will not be rescheduled.
Parameters: task - a task reference |
clearThread | protected synchronized void clearThread()(Code) | | Sets the background thread to null.
|
compare | public int compare(Object a, Object b)(Code) | | Required to back the priority queue for scheduled tasks.
Parameters: a - the first Task Parameters: b - the second Task 0 if equal, < 0 if a < b, > 0 if a > b |
getLastScheduled | public static Date getLastScheduled(Object task)(Code) | | Retrieves the last time the referenced task was executed, as a
Date object. If the task has never been executed, null is returned.
Parameters: task - a task reference the last time the referenced task was executed; null if never |
getNextScheduled | public static Date getNextScheduled(Object task)(Code) | | Retrieves the next time the referenced task is due to be executed, as a
Date object. If the referenced task is cancelled, null is returned.
Parameters: task - a task reference the next time the referenced task is due to be executed |
getThread | public synchronized Thread getThread()(Code) | | Retrieves the background execution thread.
null is returned if there is no such thread.
the current background thread (may be null) |
isCancelled | public static boolean isCancelled(Object task)(Code) | | Retrieves whether the specified argument references a cancelled task.
Parameters: task - a task reference true if referenced task is cancelled |
isFixedDelay | public static boolean isFixedDelay(Object task)(Code) | | Retrieves whether the specified argument references a task scheduled
periodically using fixed delay scheduling.
Parameters: task - a task reference true if the reference is scheduled using a fixed delay |
isFixedRate | public static boolean isFixedRate(Object task)(Code) | | Retrieves whether the specified argument references a task scheduled
periodically using fixed rate scheduling.
Parameters: task - a task reference true if the task is scheduled at a fixed rate |
isPeriodic | public static boolean isPeriodic(Object task)(Code) | | Retrieves whether the specified argument references a task scheduled
for periodic execution.
Parameters: task - a task reference true if the task is scheduled for periodic execution |
newThread | public Thread newThread(Runnable runnable)(Code) | | Default ThreadFactory implementation.
Contructs a new Thread from the designated runnable, sets its
name to "HSQLDB Timer @" + Integer.toHexString(hashCode()),
and sets it as a daemon thread.
Parameters: runnable - used to construct the new Thread. a new Thread constructed from the designated runnable. |
nextTask | protected Task nextTask()(Code) | | Retrieves the next task to execute, or null if this timer is shutdown,
the current thread is interrupted, or there are no queued tasks.
the next task to execute, or null |
scheduleAfter | public Object scheduleAfter(long delay, Runnable runnable) throws IllegalArgumentException(Code) | | Causes the specified Runnable to be executed once in the background
after the specified delay.
Parameters: delay - in milliseconds Parameters: runnable - the Runnable to execute. opaque reference to the internal task throws: IllegalArgumentException - if runnable is null |
scheduleAt | public Object scheduleAt(Date date, Runnable runnable) throws IllegalArgumentException(Code) | | Causes the specified Runnable to be executed once in the background
at the specified time.
Parameters: date - time at which to execute the specified Runnable Parameters: runnable - the Runnable to execute. opaque reference to the internal task throws: IllegalArgumentException - if date or runnable is null |
schedulePeriodicallyAfter | public Object schedulePeriodicallyAfter(long delay, long period, Runnable runnable, boolean relative) throws IllegalArgumentException(Code) | | Causes the specified Runnable to be executed periodically in the
background, starting after the specified delay.
opaque reference to the internal task Parameters: period - the cycle period Parameters: relative - if true, fixed rate sheduling else fixed delay scheduling Parameters: delay - in milliseconds Parameters: runnable - the Runnable to execute. throws: IllegalArgumentException - if runnable is null or period is <= 0 |
schedulePeriodicallyAt | public Object schedulePeriodicallyAt(Date date, long period, Runnable runnable, boolean relative) throws IllegalArgumentException(Code) | | Causes the specified Runnable to be executed periodically in the
background, starting at the specified time.
opaque reference to the internal task Parameters: period - the cycle period Parameters: relative - if true, fixed rate sheduling else fixed delay scheduling Parameters: date - time at which to execute the specified Runnable Parameters: runnable - the Runnable to execute throws: IllegalArgumentException - if date or runnable is null, orperiod is <= 0 |
setPeriod | public static Object setPeriod(Object task, long period)(Code) | | Sets the periodicity of the designated task to a new value.
If the designated task is cancelled or the new period is identical to the
task's current period, then this invocation has essentially no effect
and the submitted object is returned.
Otherwise, if the new period is greater than the designated task's
current period, then a simple assignment occurs and the submittted
object is returned.
If neither case holds, then the designated task is cancelled and a new,
equivalent task with the new period is scheduled for immediate first
execution and returned to the caller.
a task reference, as per the rules stated above. Parameters: task - the task whose periodicity is to be set Parameters: period - the new period |
shutdown | public synchronized void shutdown()(Code) | | Shuts down this timer after the current task (if any) completes.
After this call, the timer has permanently entered the shutdown state;
attempting to schedule any new task or directly restart this timer will
result in an IllegalStateException.
|
shutdownImmediately | public synchronized void shutdownImmediately()(Code) | | Shuts down this timer immediately, interrupting the wait state associated
with the current head of the task queue or the wait state internal to
the currently executing task, if any such state is currently in effect.
After this call, the timer has permanently entered the shutdown state;
attempting to schedule any new task or directly restart this timer will
result in an IllegalStateException.
Note: If the integrity of work performed by a scheduled task
may be adversely affected by an unplanned interruption, it is the
responsibility of the task's implementation to deal correctly with the
possibility that this method is called while such work is in progress,
for instance by catching the InterruptedException, completing the work,
and then rethrowing the exception.
|
|
|