vmm.core
Class TaskManager

java.lang.Object
  extended by vmm.core.TaskManager

public class TaskManager
extends java.lang.Object

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

TaskManager

public TaskManager()
Create a TaskManager that will use a pool of threads with one thread per available processor.

See Also:
TaskManager(int)

TaskManager

public TaskManager(int threadPoolSize)
Create a TaskManager that will use a pool of threads with a specified number of threads. The threads are used to execute "jobs", where a job consists of a collection of Runnable objects. Note that even a thread pool with just one thread can be useful for asynchronous execution.

Parameters:
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

shutDown

public void shutDown()
This method should be called before discarding the TaskManager. Any jobs that have not been completed are cancled (using 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.


getThreadPoolSize

public int getThreadPoolSize()
Returns the number of threads that will be used in the thread pool. The value was set in the constructor and does not change. The return value is a positive integer.


executeAndWait

public void executeAndWait(java.util.Collection<? extends java.lang.Runnable> tasks)
Executes all the tasks in a collection of tasks. This method does not return until all the tasks have finished, either by terminating normally or by throwing an exception. If the thread pool size is 1, all the tasks are executed in the calling thread, without the use of any additional threads. If the thread pool size is greater than 1, all the threads in the pool are used to execute the tasks, resulting in some parallelization.

Parameters:
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.
Throws:
java.lang.IllegalStateException - if this method is called after shutDown() has been called.
java.lang.NullPointerException - if the argument is null
See Also:
getThreadPoolSize()

executeAsync

public 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. This method returns immedialtely, and the execution of the tasks procedes asynchronously. The tasks are executed using the TaskManager's thread pool.

Parameters:
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.
Returns:
a "job" object that can be used to get status information about the job and to wait for the job to be completed. It is not possible to add additional tasks to this job.
Throws:
java.lang.IllegalStateException - if this method is called after shutDown() has been called.
java.lang.NullPointerException - if the argument is null

createJob

public TaskManager.Job createJob()
Creates a "job" to which a collection of tasks can be added. See 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.

Returns:
the job object. This can be used to add tasks to the job, to get status information about the job, and to wait for the job to complete.
Throws:
java.lang.IllegalStateException - if this method is called after shutDown() has been called.

busy

public boolean busy()
Tells whether this TaskManager has at least one job that has not yet finished. Note that the TaskManager might not really be doing anyting, if none of the jobs have tasks that need to be performed.