vmm.core
Class TaskManager.Job

java.lang.Object
  extended by vmm.core.TaskManager.Job
Enclosing class:
TaskManager

public static class TaskManager.Job
extends java.lang.Object

Represents a job that consists of the execution of a number of tasks. It is not possible to create an object of this class directly. Objects of type TaskManager.Job are returned by TaskManager.executeAsync(Collection) and TaskManager.createJob(). The tasks that are part of a job will be exectued by the TaskManager that created the job, using the pool of threads in that TaskManager. A job can start executing as soon as it has been created and the first task has been added.

See Also:
TaskManager

Method Summary
 void add(java.lang.Runnable task)
          Add a task to this job.
 boolean await(int timeoutMilliseconds)
          Waits either a specified amount of time or indefinitely for this job to finish.
 void cancel()
          Cancel the job.
 void close()
          "Close" this job, making it possible for the job to complete.
 int failedTaskCount()
          Returns the number of tasks in this job that have been terminated because of an exception.
 java.lang.Runnable[] failedTasks()
          Returns an array that contains tasks from this job that have terminated because of an Error or Exception.
 int finishedTaskCount()
          Returns the number of tasks in this job that have been completed successfully.
 java.lang.Runnable[] finishedTasks()
          Returns an array that contains tasks from this job that have completed successfully.
 double fractionDone()
          Tells the fraction of tasks that have been added to this job that have been completed.
 boolean isCanceled()
          Tells whether hte job has been canceled.
 boolean isFinished()
          Tells whether the job is finished.
 int totalTaskCount()
          Returns the number of tasks that have been added to this job.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

add

public void add(java.lang.Runnable task)
Add a task to this job. The job will not finish until all the tasks that have been added to the job have finished (successfully or because of an exception). Also, the job must be closed before it can finish. Note that tasks cannot be added to a job that has is "closed". See close().

Parameters:
task - the task that is to be added to this job. A null value is ignored.
Throws:
java.lang.IllegalStateException - if the job has already been closed.

close

public void close()
"Close" this job, making it possible for the job to complete. Closing a job also makes it impossible to add new tasks to the job. A job that was created using TaskManager.executeAsync(Collection) is already closed when it is returned by that method. A job that was created using TaskManager.createJob() must be closed, or it will be impossible for that job to finish; the job should be closed by calling this method after all the tasks that are part of the job have been added.


fractionDone

public double fractionDone()
Tells the fraction of tasks that have been added to this job that have been completed.

Returns:
a number between 0 and 1 obtained by dividing the number of completed tasks by the number of tasks that have been added. If no tasks have been added, the return value is 1. Note that the fractionDone can go down, if more tasks are added to the job.

finishedTaskCount

public int finishedTaskCount()
Returns the number of tasks in this job that have been completed successfully.


failedTaskCount

public int failedTaskCount()
Returns the number of tasks in this job that have been terminated because of an exception.


totalTaskCount

public int totalTaskCount()
Returns the number of tasks that have been added to this job.


cancel

public void cancel()
Cancel the job. Tasks that have not yet been started will not be discarded; however, tasks that are in progress can run to completion and might finish after this method returns. This method can be called to cancel a job even if that job has not yet been closed.


isFinished

public boolean isFinished()
Tells whether the job is finished. A job is finished either when all the tasks that are part of the job are done or when the job has been canceled.


isCanceled

public boolean isCanceled()
Tells whether hte job has been canceled. A job can be canceled by calling cancel().


finishedTasks

public java.lang.Runnable[] finishedTasks()
Returns an array that contains tasks from this job that have completed successfully. If this method has been called previously, only the newly completed tasks, since the last call, are returned. The return value can be an empty array, if there are no newly completed tasks, but the return value is never null. This method can be used to retreive completed tasks for further processing.


failedTasks

public java.lang.Runnable[] failedTasks()
Returns an array that contains tasks from this job that have terminated because of an Error or Exception. If this method has been called previously, only the newly completed tasks, since the last call, are returned. The return value is non-null but can be empty.


await

public boolean await(int timeoutMilliseconds)
Waits either a specified amount of time or indefinitely for this job to finish. The method will return only after the job completes or after the specified timeout if the job does not complete within that time. The return value tells whether or not the job has completed. If the job is already complete when this method is called, it returns immedialtely.

Parameters:
timeoutMilliseconds - the maximum time to wait for the job to finish. A value of 0 (or less) means to wait as long as it takes for the job to finish.
Returns:
true if the job has finished, false if not. Note that if the argument is less than or equal to 0, then the return value has to be true.