|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.Object
|
+--java.lang.Thread
|
+--org.sapia.taskman.TaskManager
An instance of this class executes Task instances. An new task
is added to the task manager through a TaskDescriptor. The following
excerpt shows how to use a task manager:
// The following adds a periodic task that is executed every 5 seconds.
TaskManager mgr = new TaskManager();
TaskDescriptor periodicTask = new PeriodicTaskDescriptor("periodic", 5000, new MyTask());
mgr.execTaskFor(periodicTask);
mgr.start();
// Tasks can be added after the task manager has started:
TaskDescriptor transientTask = new TransientTaskDescriptor("transient task", new MyTask());
mgr.execTaskFor(transientTask);
// The above adds a "transient task" - a task that will be executed only once.
The above code demonstrates how to add asynchronous tasks: tasks are added, and "eventually"
executed. Yet, tasks can also be executed synchronously:
TaskManager mgr = new TaskManager();
mgr.start();
mgr.execSyncTask("synchronous", new MyTask());
// or
mgr.execSyncTask("synchronous", new MyTask(), myOutput);
In the above snippet, the second case shows that it is possible for the caller
to pass an instance of TaskOutput as a parameter. This allows a
caller to trap task output information and send it where it pleases.
A task manager internally processes its list of tasks to execute and executes
them one by one. Once the list has been processed, the task manager goes to
sleep (in order not to consume precious cpu cycles if no task needs being executed
for a given while) - this sleeping delay can be configured through the
setRunInterval() method.
More specifically, the task manager behaves in the following way: if its task list is empty, the task manager waits for a new task to be added before waking up; if the task list is not empty, the task manager waits for the next time a task is ready for execution, or for the addition of a new task.
| Field Summary | |
static long |
RUN_INTERVAL
|
| Fields inherited from class java.lang.Thread |
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY |
| Constructor Summary | |
TaskManager()
Creates a new TaskManager. |
|
TaskManager(String name)
Creates an instance of this class with the given name. |
|
| Method Summary | |
void |
addTaskDescriptors(List tasks)
Adds the given list of task descriptors to this instance. |
void |
execAsyncTask(String name,
Task t,
TaskOutput out)
Executes the given task asynchronously. |
void |
execSyncTask(String name,
Task t)
Executes the given task synchronously. |
void |
execSyncTask(String name,
Task t,
TaskOutput out)
Executes the given task synchronously. |
void |
execTaskFor(TaskDescriptor desc)
Asynchronously executes the task corresponding to the given descriptor. |
List |
getTaskDescriptors()
Returns this instance's internal list of task descriptors in another list. |
protected TaskOutput |
newTaskOutput(String taskName)
This template method is called every time a TaskOutput
is created and associated with a Task. |
void |
readExternal(ObjectInput in)
|
void |
run()
|
void |
setRunInterval(long millis)
Sets the interval at which this instance will wake up to process its list of internal tasks. |
void |
shutdown()
Shuts down this instance. |
void |
writeExternal(ObjectOutput out)
|
| Methods inherited from class java.lang.Thread |
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, 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 |
public static final long RUN_INTERVAL
| Constructor Detail |
public TaskManager()
TaskManager.
public TaskManager(String name)
name - a name.| Method Detail |
public void addTaskDescriptors(List tasks)
public List getTaskDescriptors()
List of TaskDescriptorpublic void setRunInterval(long millis)
millis - a time interval in milliseconds.public void execTaskFor(TaskDescriptor desc)
desc - a TaskDescriptor
public void execSyncTask(String name,
Task t)
name - the name of the task - will be passed to the
TaskOutput that will be associated with the task.t - a Task.
public void execSyncTask(String name,
Task t,
TaskOutput out)
name - the name of the task.t - a Task.out - a TaskOutput.
public void execAsyncTask(String name,
Task t,
TaskOutput out)
TaskOutput.
The calling thread can thus receive task output asynchronously.
name - the name of the task.t - a Task.out - a TaskOutput.public void shutdown()
public void readExternal(ObjectInput in)
throws IOException,
ClassNotFoundException
readExternal in interface ExternalizableIOException
ClassNotFoundExceptionExternalizable.readExternal(ObjectInput)
public void writeExternal(ObjectOutput out)
throws IOException
writeExternal in interface ExternalizableIOExceptionExternalizable.writeExternal(ObjectOutput)protected TaskOutput newTaskOutput(String taskName)
TaskOutput
is created and associated with a Task.
taskName - the name of the task for which to create a TaskOutput.public void run()
run in interface Runnablerun in class ThreadThread.run()
|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||