Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.ematgine.utils.concurrent
Class TimeDaemon  view TimeDaemon download TimeDaemon.java

java.lang.Object
  extended byorg.ematgine.utils.concurrent.ThreadFactoryUser
      extended byorg.ematgine.utils.concurrent.TimeDaemon

public class TimeDaemon
extends ThreadFactoryUser

A general-purpose timer daemon, vaguely similar in functionality common system-level utilities like 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 the next request.

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.

[ Introduction to this package. ]


Nested Class Summary
protected  class TimeDaemon.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 would never make sense here.
protected static class TimeDaemon.TaskNode
           
 
Nested classes inherited from class org.ematgine.utils.concurrent.ThreadFactoryUser
ThreadFactoryUser.DefaultThreadFactory
 
Field Summary
protected  Heap heap_
          tasks are maintained in a standard priority queue
protected  TimeDaemon.RunLoop runLoop_
           
protected  java.lang.Thread thread_
          The thread used to process commands
 
Fields inherited from class org.ematgine.utils.concurrent.ThreadFactoryUser
threadFactory_
 
Constructor Summary
TimeDaemon()
          Create a new TimeDaemon
 
Method Summary
static void cancel(java.lang.Object taskID)
          Cancel a scheduled task.
protected  void clearThread()
          set thread_ to null to indicate termination
 java.lang.Object executeAfterDelay(long millisecondsToDelay, java.lang.Runnable command)
          Excecute the given command after waiting for the given delay.
 java.lang.Object executeAt(java.util.Date date, java.lang.Runnable command)
          Execute the given command at the given time.
 java.lang.Object executePeriodically(long period, java.lang.Runnable command, boolean startNow)
          Execute the given command every period milliseconds.
 java.lang.Thread getThread()
          Return the thread being used to process commands, or null if there is no such thread.
protected  TimeDaemon.TaskNode nextTask()
          Return the next task to execute, or null if thread is interrupted
protected  void restart()
          Start (or restart) a thread to process commands, or wake up an existing thread if one is already running.
 void shutDown()
          Cancel all tasks and interrupt the background thread executing the current task, if any.
 
Methods inherited from class org.ematgine.utils.concurrent.ThreadFactoryUser
getThreadFactory, setThreadFactory
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

heap_

protected final Heap heap_
tasks are maintained in a standard priority queue


thread_

protected java.lang.Thread thread_
The thread used to process commands


runLoop_

protected final TimeDaemon.RunLoop runLoop_
Constructor Detail

TimeDaemon

public TimeDaemon()
Create a new TimeDaemon

Method Detail

executeAt

public java.lang.Object executeAt(java.util.Date date,
                                  java.lang.Runnable command)
Execute the given command at the given time.


executeAfterDelay

public java.lang.Object executeAfterDelay(long millisecondsToDelay,
                                          java.lang.Runnable command)
Excecute the given command after waiting for the given delay.


executePeriodically

public java.lang.Object executePeriodically(long period,
                                            java.lang.Runnable command,
                                            boolean startNow)
Execute the given command every period milliseconds. If startNow is true, execution begins immediately, otherwise, it begins after the first period delay.


cancel

public static void cancel(java.lang.Object taskID)
Cancel a scheduled task. The task will be cancelled upon the next opportunity to run it. This has no effect if this is a one-shot task that has already executed. If an execution is in progress, it will complete normally, but if it is a periodic task, future iterations are cancelled.


getThread

public java.lang.Thread getThread()
Return the thread being used to process commands, or null if there is no such thread. You can use this to invoke any special methods on the thread, for example, to interrupt it.


clearThread

protected void clearThread()
set thread_ to null to indicate termination


restart

protected void restart()
Start (or restart) a thread to process commands, or wake up an existing thread if one is already running.


shutDown

public void shutDown()
Cancel all tasks and interrupt the background thread executing the current task, if any. (A new background thread will be started if new execution requests are encountered.)


nextTask

protected TimeDaemon.TaskNode nextTask()
Return the next task to execute, or null if thread is interrupted