java.lang.ObjectA general-purpose time-based daemon, vaguely similar in functionality to common system-level utilities such asEDU.oswego.cs.dl.util.concurrent.ThreadFactoryUser
EDU.oswego.cs.dl.util.concurrent.ClockDaemon
at
(and the associated crond) in Unix.
Objects of this class maintain a single thread and a task queue
that may be used to execute Runnable commands in any of three modes --
absolute (run at a given time), relative (run after a given delay),
and periodic (cyclically run with a given delay).
All commands are executed by the single background thread.
The thread is not actually started until the first
request is encountered. Also, if the
thread is stopped for any reason, one is started upon encountering
the next request, or restart()
is invoked.
If you would instead like commands run in their own threads, you can use as arguments Runnable commands that start their own threads (or perhaps wrap within ThreadedExecutors).
You can also use multiple daemon objects, each using a different background thread. However, one of the reasons for using a time daemon is to pool together processing of infrequent tasks using a single background thread.
Background threads are created using a ThreadFactory. The
default factory does not
automatically setDaemon
status.
The class uses Java timed waits for scheduling. These can vary in precision across platforms, and provide no real-time guarantees about meeting deadlines.
[ Introduction to this package. ]
Nested Class Summary: | ||
---|---|---|
protected static class | ClockDaemon.TaskNode | |
protected class | ClockDaemon.RunLoop | The runloop is isolated in its own Runnable class just so that the main class need not implement Runnable, which would allow others to directly invoke run, which is not supported. * |
Field Summary | ||
---|---|---|
protected final Heap | heap_ | tasks are maintained in a standard priority queue |
protected Thread | thread_ | The thread used to process commands * |
protected final RunLoop | runLoop_ |
Fields inherited from EDU.oswego.cs.dl.util.concurrent.ThreadFactoryUser: |
---|
threadFactory_ |
Constructor: |
---|
|
Method from EDU.oswego.cs.dl.util.concurrent.ClockDaemon Summary: |
---|
cancel, clearThread, executeAfterDelay, executeAt, executePeriodically, getThread, nextTask, restart, shutDown |
Methods from EDU.oswego.cs.dl.util.concurrent.ThreadFactoryUser: |
---|
getThreadFactory, setThreadFactory |
Methods from java.lang.Object: |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method from EDU.oswego.cs.dl.util.concurrent.ClockDaemon Detail: |
---|
|
|
Sample Usage. You can use a ClockDaemon to arrange timeout callbacks to break out of stuck IO. For example (code sketch): class X { ... ClockDaemon timer = ... Thread readerThread; FileInputStream datafile; void startReadThread() { datafile = new FileInputStream("data", ...); readerThread = new Thread(new Runnable() { public void run() { for(;;) { // try to gracefully exit before blocking if (Thread.currentThread().isInterrupted()) { quietlyWrapUpAndReturn(); } else { try { int c = datafile.read(); if (c == -1) break; else process(c); } catch (IOException ex) { cleanup(); return; } } } }; readerThread.start(); // establish callback to cancel after 60 seconds timer.executeAfterDelay(60000, new Runnable() { readerThread.interrupt(); // try to interrupt thread datafile.close(); // force thread to lose its input file }); } } |
|
period milliseconds.
If startNow is true, execution begins immediately,
otherwise, it begins after the first period delay.
Sample Usage. Here is one way to update Swing components acting as progress indicators for long-running actions. class X { JLabel statusLabel = ...; int percentComplete = 0; synchronized int getPercentComplete() { return percentComplete; } synchronized void setPercentComplete(int p) { percentComplete = p; } ClockDaemon cd = ...; void startWorking() { Runnable showPct = new Runnable() { public void run() { SwingUtilities.invokeLater(new Runnable() { public void run() { statusLabel.setText(getPercentComplete() + "%"); } } } }; final Object updater = cd.executePeriodically(500, showPct, true); Runnable action = new Runnable() { public void run() { for (int i = 0; i < 100; ++i) { work(); setPercentComplete(i); } cd.cancel(updater); } }; new Thread(action).start(); } } |
|
|
|
|