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.
| Modifier and Type | Class and Description |
|---|---|
| protected class | HsqlTimer.
Encapsulates a Runnable and its scheduling attributes. |
| protected static class | HsqlTimer.
Heap-based priority queue. |
| protected class | HsqlTimer.
The Runnable that the background thread uses to execute scheduled tasks. |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv volatile boolean | isShutdown
Whether this timer should disallow all further processing. |
| pack-priv static int | nowCount
stats var |
| pack-priv final HsqlTimer. | taskQueue
The priority queue for the scheduled tasks. |
| pack-priv final HsqlTimer. | taskRunner
The inner runnable that executes tasks in the background thread. |
| pack-priv Thread | taskRunnerThread
The background thread. |
| pack-priv final ThreadFactory | threadFactory
The factory that produces the background threads. |
| Access | Constructor and Description |
|---|---|
| public | |
| public | HsqlTimer(final ThreadFactory
the ThreadFactory used to produce this timer's
background threads. If null, the default implementation supplied
by this class will be used. threadFactory)Constructs a new HsqlTimer. |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv HsqlTimer. | Returns: an opaque reference to the internal taskthe time of the task's first execution first, final Runnable the Runnable to execute runnable, final long the task's periodicity period, boolean if true, use fixed rate else use fixed delay scheduling relative)Adds to the task queue a new Task object encapsulating the supplied Runnable and scheduling arguments. |
| public static void | |
| pack-priv synchronized void | |
| public int | Returns: 0 if equal, < 0 if a < b, > 0 if a > bthe first Task a, final Object the second Task b)Implements java. Required to back the priority queue for scheduled tasks. |
| public static Date | Returns: the last time the referenced task was executed; null if nevera task reference task)Retrieves the last time the referenced task was executed, as a Date object. |
| public static Date | Returns: the next time the referenced task is due to be executeda task reference task)Retrieves the next time the referenced task is due to be executed, as a Date object. |
| public synchronized Thread | Returns: the current background thread (may be null)Retrieves the background execution thread. |
| public static boolean | Returns: true if referenced task is cancelleda task reference task)Retrieves whether the specified argument references a cancelled task. |
| public static boolean | Returns: true if the reference is scheduled using a fixed delaya task reference task)Retrieves whether the specified argument references a task scheduled periodically using fixed delay scheduling. |
| public static boolean | Returns: true if the task is scheduled at a fixed ratea task reference task)Retrieves whether the specified argument references a task scheduled periodically using fixed rate scheduling. |
| public static boolean | Returns: true if the task is scheduled for periodic executiona task reference task)Retrieves whether the specified argument references a task scheduled for periodic execution. |
| public Thread | Returns: a new Thread constructed from the designated runnable.used to construct the new Thread. runnable)Implements org. Default ThreadFactory implementation. |
| pack-priv HsqlTimer. | Returns: the next task to execute, or nullRetrieves the next task to execute, or null if this timer is shutdown, the current thread is interrupted, or there are no queued tasks. |
| pack-priv static long | Returns: System.currentTimeMillis()Convenience method replacing the longer incantation: System.currentTimeMillis() |
| public synchronized void | |
| public Object | Returns: opaque reference to the internal taskin milliseconds delay, final Runnable the Runnable to execute. runnable)Causes the specified Runnable to be executed once in the background after the specified delay. |
| public Object | Returns: opaque reference to the internal tasktime at which to execute the specified Runnable date, final Runnable the Runnable to execute. runnable)Causes the specified Runnable to be executed once in the background at the specified time. |
| public Object | Returns: opaque reference to the internal taskin milliseconds delay, final long the cycle period period, final Runnable the Runnable to execute. runnable, final boolean if true, fixed rate scheduling else fixed delay scheduling relative)Causes the specified Runnable to be executed periodically in the background, starting after the specified delay. |
| public Object | Returns: opaque reference to the internal tasktime at which to execute the specified Runnable date, final long the cycle period period, final Runnable the Runnable to execute runnable, final boolean if true, fixed rate scheduling else fixed delay scheduling relative)Causes the specified Runnable to be executed periodically in the background, starting at the specified time. |
| public static Object | |
| public synchronized void | |
| public synchronized void | |
| 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. |
| isShutdown | back to summary |
|---|---|
| pack-priv volatile boolean isShutdown Whether this timer should disallow all further processing. Once set true, stays true forever. | |
| nowCount | back to summary |
|---|---|
| pack-priv static int nowCount stats var | |
| taskQueue | back to summary |
|---|---|
| pack-priv final HsqlTimer. The priority queue for the scheduled tasks. | |
| taskRunner | back to summary |
|---|---|
| pack-priv final HsqlTimer. The inner runnable that executes tasks in the background thread. | |
| taskRunnerThread | back to summary |
|---|---|
| pack-priv Thread taskRunnerThread The background thread. | |
| threadFactory | back to summary |
|---|---|
| pack-priv final ThreadFactory threadFactory The factory that produces the background threads. | |
| HsqlTimer | back to summary |
|---|---|
| public HsqlTimer() Constructs a new HsqlTimer using the default thread factory implementation. | |
| HsqlTimer | back to summary |
|---|---|
| public HsqlTimer(final ThreadFactory threadFactory) Constructs a new HsqlTimer. Uses the specified thread factory implementation.
| |
| addTask | back to summary |
|---|---|
| pack-priv HsqlTimer. Adds to the task queue a new Task object encapsulating the supplied Runnable and scheduling arguments. | |
| cancel | back to summary |
|---|---|
| public static void cancel(final Object task) 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.
| |
| clearThread | back to summary |
|---|---|
| pack-priv synchronized void clearThread() Sets the background thread to null. | |
| compare | back to summary |
|---|---|
| public int compare(final Object a, final Object b) Implements java. Required to back the priority queue for scheduled tasks. | |
| getLastScheduled | back to summary |
|---|---|
| public static Date getLastScheduled(Object task) Retrieves the last time the referenced task was executed, as a Date object. If the task has never been executed, null is returned. | |
| getNextScheduled | back to summary |
|---|---|
| public static Date getNextScheduled(Object task) 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. | |
| getThread | back to summary |
|---|---|
| public synchronized Thread getThread() Retrieves the background execution thread. null is returned if there is no such thread.
| |
| isCancelled | back to summary |
|---|---|
| public static boolean isCancelled(final Object task) Retrieves whether the specified argument references a cancelled task.
| |
| isFixedDelay | back to summary |
|---|---|
| public static boolean isFixedDelay(final Object task) Retrieves whether the specified argument references a task scheduled periodically using fixed delay scheduling.
| |
| isFixedRate | back to summary |
|---|---|
| public static boolean isFixedRate(final Object task) Retrieves whether the specified argument references a task scheduled periodically using fixed rate scheduling.
| |
| isPeriodic | back to summary |
|---|---|
| public static boolean isPeriodic(final Object task) Retrieves whether the specified argument references a task scheduled for periodic execution.
| |
| newThread | back to summary |
|---|---|
| public Thread newThread(final Runnable runnable) Implements org. Default ThreadFactory implementation. Constructs a new Thread from the designated runnable, sets its name to "HSQLDB Timer @" + Integer.toHexString(hashCode()), and sets it as a daemon thread.
| |
| nextTask | back to summary |
|---|---|
| pack-priv HsqlTimer. Retrieves the next task to execute, or null if this timer is shutdown, the current thread is interrupted, or there are no queued tasks. | |
| now | back to summary |
|---|---|
| pack-priv static long now() Convenience method replacing the longer incantation: System.currentTimeMillis()
| |
| restart | back to summary |
|---|---|
| public synchronized void restart() throws IllegalStateException (Re)starts background processing of the task queue.
| |
| scheduleAfter | back to summary |
|---|---|
| public Object scheduleAfter(final long delay, final Runnable runnable) throws IllegalArgumentException Causes the specified Runnable to be executed once in the background after the specified delay.
| |
| scheduleAt | back to summary |
|---|---|
| public Object scheduleAt(final Date date, final Runnable runnable) throws IllegalArgumentException Causes the specified Runnable to be executed once in the background at the specified time.
| |
| schedulePeriodicallyAfter | back to summary |
|---|---|
| public Object schedulePeriodicallyAfter(final long delay, final long period, final Runnable runnable, final boolean relative) throws IllegalArgumentException Causes the specified Runnable to be executed periodically in the background, starting after the specified delay.
| |
| schedulePeriodicallyAt | back to summary |
|---|---|
| public Object schedulePeriodicallyAt(final Date date, final long period, final Runnable runnable, final boolean relative) throws IllegalArgumentException Causes the specified Runnable to be executed periodically in the background, starting at the specified time.
| |
| setPeriod | back to summary |
|---|---|
| public static Object setPeriod(final Object task, final long period) 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 submitted 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.
| |
| shutdown | back to summary |
|---|---|
| public synchronized void shutdown() 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. | |
| shutDown | back to summary |
|---|---|
| public synchronized void shutDown() for compatibility with previous version | |
| shutdownImmediately | back to summary |
|---|---|
| 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. 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. | |
| Modifier and Type | Field and Description |
|---|---|
| private final Object | cancel_mutex
Serializes concurrent access to the cancelled field. |
| pack-priv boolean | cancelled
Whether to silently remove this task instead of running it, the next time (if ever) it makes its way to the head of the timer queue. |
| pack-priv long | last
The time this task was last executed, or 0 if never. |
| pack-priv long | next
The next time this task is scheduled to execute. |
| pack-priv long | period
The periodic interval, or 0 if one-shot. |
| pack-priv final boolean | relative
Scheduling policy flag. |
| pack-priv Runnable | runnable
What to run. |
| Access | Constructor and Description |
|---|---|
| pack-priv | Task(final long
the first time to execute first, final Runnable the Runnable to execute runnable, final long the periodicity of execution period, final boolean if true, use fixed rate scheduling else fixed delay relative)Constructs a new Task object encapsulating the specified Runnable and scheduling arguments. |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv void | |
| pack-priv synchronized long | Returns: the last time this task was executed or zero if neverRetrieves the instant in time just before this task was last executed by the background thread. |
| pack-priv synchronized long | Returns: the time at which this task is next scheduled for executionRetrieves the time at which this task is next scheduled for execution. |
| pack-priv boolean | |
| pack-priv synchronized Object | Returns: a task reference, as per the rules stated above.the new period newPeriod)Sets the new periodicity of this task in milliseconds. |
| pack-priv synchronized void | updateSchedule(final long
when this task was last executed last, final long when this task is to be next executed next)Updates the last and next scheduled execution times. |
| cancel_mutex | back to summary |
|---|---|
| private final Object cancel_mutex Serializes concurrent access to the cancelled field. | |
| cancelled | back to summary |
|---|---|
| pack-priv boolean cancelled Whether to silently remove this task instead of running it, the next time (if ever) it makes its way to the head of the timer queue. | |
| last | back to summary |
|---|---|
| pack-priv long last The time this task was last executed, or 0 if never. | |
| next | back to summary |
|---|---|
| pack-priv long next The next time this task is scheduled to execute. | |
| period | back to summary |
|---|---|
| pack-priv long period The periodic interval, or 0 if one-shot. | |
| relative | back to summary |
|---|---|
| pack-priv final boolean relative Scheduling policy flag. When true, scheduling is fixed rate (as opposed to fixed delay), and schedule updates are calculated relative to when the task was last run rather than a fixed delay starting from the current wall-clock time provided by System.currentTimeMillis(). This helps normalize scheduling for tasks that must attempt to maintain a fixed rate of execution. | |
| runnable | back to summary |
|---|---|
| pack-priv Runnable runnable What to run. | |
| Task | back to summary |
|---|---|
| pack-priv Task(final long first, final Runnable runnable, final long period, final boolean relative) Constructs a new Task object encapsulating the specified Runnable and scheduling arguments.
| |
| cancel | back to summary |
|---|---|
| pack-priv void cancel() Sets this task's cancelled flag true and signals its taskQueue. | |
| getLastScheduled | back to summary |
|---|---|
| pack-priv synchronized long getLastScheduled() Retrieves the instant in time just before this task was last executed by the background thread. A value of zero indicates that this task has never been executed.
| |
| getNextScheduled | back to summary |
|---|---|
| pack-priv synchronized long getNextScheduled() Retrieves the time at which this task is next scheduled for execution.
| |
| isCancelled | back to summary |
|---|---|
| pack-priv boolean isCancelled() Retrieves whether this task is cancelled.
| |
| setPeriod | back to summary |
|---|---|
| pack-priv synchronized Object setPeriod(final long newPeriod) Sets the new periodicity of this task in milliseconds. If this task is cancelled or the new period is identical to the current period, then this invocation has essentially no effect and this object is returned. Otherwise, if the new period is greater than the current period, then a simple field assignment occurs and this object is returned. If none of the previous cases hold, then this task is cancelled and a new, equivalent task with the new period is scheduled for immediate first execution and returned to the caller.
| |
| updateSchedule | back to summary |
|---|---|
| pack-priv synchronized void updateSchedule(final long last, final long next) Updates the last and next scheduled execution times.
| |
| Access | Constructor and Description |
|---|---|
| pack-priv | TaskQueue(final int
the initial capacity of the queue capacity, final Comparator<HsqlTimer.The ObjectComparator this queue uses to maintain its
Heap invariant. ocConstructs a new TaskQueue with the specified initial capacity and ObjectComparator. |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv void | |
| pack-priv void | |
| pack-priv synchronized void | |
| pack-priv synchronized HsqlTimer. | Returns: the earliest scheduled non-cancelled task, or null if no such task existsRetrieves the head of this queue, without removing it. |
| pack-priv HsqlTimer. | |
| pack-priv synchronized void | signalTaskCancelled(HsqlTimer.
a supposedly cancelled task taskInforms this queue that the given task is supposedly cancelled. |
| pack-priv synchronized void |
| TaskQueue | back to summary |
|---|---|
| pack-priv TaskQueue(final int capacity, final Comparator<HsqlTimer. Constructs a new TaskQueue with the specified initial capacity and ObjectComparator.
| |
| addTask | back to summary |
|---|---|
| pack-priv void addTask(final HsqlTimer. Type-safe add method. Can be used to inject debugging or accounting behaviour.
| |
| cancelAllTasks | back to summary |
|---|---|
| pack-priv void cancelAllTasks() Atomically removes all tasks in this queue and then and cancels them. | |
| park | back to summary |
|---|---|
| pack-priv synchronized void park(final long timeout) throws InterruptedException Causes the calling thread to wait until another thread invokes
| |
| peekTask | back to summary |
|---|---|
| pack-priv synchronized HsqlTimer. Retrieves the head of this queue, without removing it. This method has the side effect of removing tasks from the head of this queue until a non-cancelled task is encountered or this queue is empty. If this queue is initially empty or is emptied in the process of finding the earliest scheduled non-cancelled task, then null is returned.
| |
| removeTask | back to summary |
|---|---|
| pack-priv HsqlTimer. Type-safe remove method. Removes the head task from this queue. Can be used to inject debugging or accounting behaviour.
| |
| signalTaskCancelled | back to summary |
|---|---|
| pack-priv synchronized void signalTaskCancelled(HsqlTimer. Informs this queue that the given task is supposedly cancelled.
If the indicated task is identical to the current head of
this queue, then it is removed and this queue is
The cancelled status of the given task is not verified; it is assumed that the caller is well-behaved (always passes a non-null reference to a cancelled task). | |
| unpark | back to summary |
|---|---|
| pack-priv synchronized void unpark() Wakes up a single thread (if any) that is waiting on this queue's
| |
Note
Outer class could simply implement Runnable, but using an inner class protects the public run method from potential abuse.
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public void |
| TaskRunner | back to summary |
|---|---|
| protected TaskRunner() | |
| run | back to summary |
|---|---|
| public void run() Implements java. Runs the next available task in the background thread. When there are no available tasks, the background thread dies and its instance field is cleared until tasks once again become available. | |