|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object vmm.core.TaskManager
public class TaskManager
Provides parallization for a collection of tasks, where each task is an object of type Runnable. The tasks can be run by a pool of threads, where the size of the pool can be specified in the TaskManager constructor. The default is for the number ofthreads to be equal to the number of processors.
There are three ways to run a collection of tasks: executeAndWait(Collection)
will run all the task in a collection of Runnable objects and will return only
when all the tasks have finished. executeAsync(Collection)
will run
the tasks asynchronously. The return value of this method is an object of
type TaskManager.Job
; this "job" object can be used to get status infomation
about the job, to cancel the job, and to wait for the job to finish.
createJob()
also returns an object of type TaskManager.Job, but in this
case the job does not initially have any tasks to do. Tasks can be added by
calling TaskManager.Job.add(Runnable)
. After all tasks that are part of
the job have been added, TaskManager.Job.close()
must be called to
indicate that the job is complete. (Note that it is not possible to
add additional tasks to a job that has been created using executeAsync(Collection)
.)
Nested Class Summary | |
---|---|
static class |
TaskManager.Job
Represents a job that consists of the execution of a number of tasks. |
Constructor Summary | |
---|---|
TaskManager()
Create a TaskManager that will use a pool of threads with one thread per available processor. |
|
TaskManager(int threadPoolSize)
Create a TaskManager that will use a pool of threads with a specified number of threads. |
Method Summary | |
---|---|
boolean |
busy()
Tells whether this TaskManager has at least one job that has not yet finished. |
TaskManager.Job |
createJob()
Creates a "job" to which a collection of tasks can be added. |
void |
executeAndWait(java.util.Collection<? extends java.lang.Runnable> tasks)
Executes all the tasks in a collection of tasks. |
TaskManager.Job |
executeAsync(java.util.Collection<? extends java.lang.Runnable> tasks)
Creates a job to execute a specified collection of tasks, and starts working on the job. |
int |
getThreadPoolSize()
Returns the number of threads that will be used in the thread pool. |
void |
shutDown()
This method should be called before discarding the TaskManager. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public TaskManager()
TaskManager(int)
public TaskManager(int threadPoolSize)
threadPoolSize
- the number of thread to be used. If the value is 0 (or less),
then the number of threads will be equal to the number of available processors.Method Detail |
---|
public void shutDown()
TaskManager.Job.cancel()
).
Then the threads in the thread pool are allowed to die. It is not possible
to add new jobs to a TaskManager after the TaskManager has been shut down.
public int getThreadPoolSize()
public void executeAndWait(java.util.Collection<? extends java.lang.Runnable> tasks)
tasks
- the tasks to be performed. Must be non-null. Each task in the collection
is an object of type Runnable. Null entries in the collection are ignored. (Note that the
actual parameter can be (for example) an ArrayList declared as of type
ArrayList<Runnable> or ArrayList<Type> where Type is a class
that implements the Runnable interface.
java.lang.IllegalStateException
- if this method is called after shutDown()
has been called.
java.lang.NullPointerException
- if the argument is nullgetThreadPoolSize()
public TaskManager.Job executeAsync(java.util.Collection<? extends java.lang.Runnable> tasks)
tasks
- a non-null collection of tasks to be executed. Each task is an object
of that implements the Runnable interface. Null tasks are ignored.
java.lang.IllegalStateException
- if this method is called after shutDown()
has been called.
java.lang.NullPointerException
- if the argument is nullpublic TaskManager.Job createJob()
TaskManager.Job.add(Runnable)
.
The job must be "closed," using TaskManager.Job.close()
after all the tasks have been added,
or the job will never complete.
java.lang.IllegalStateException
- if this method is called after shutDown()
has been called.public boolean busy()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |