pt.runtime
Class TaskID<E>

java.lang.Object
  extended by pt.runtime.TaskID<E>
Type Parameters:
E - The task's return type
Direct Known Subclasses:
TaskIDGroup

public class TaskID<E>
extends java.lang.Object

A future object representing a task invocation. As well as containing the return result for non-void tasks, the TaskID may also be used with dependsOn, cancel attempts, and other various functions.

Author:
Nasser Giacaman, Oliver Sinnen

Field Summary
protected  boolean cancelled
           
protected static int CANCELLED
           
protected  java.util.concurrent.atomic.AtomicBoolean cancelRequested
           
protected static int CREATED
           
protected  TaskID<?> enclosingTask
           
protected  int globalID
           
protected  TaskIDGroup group
           
protected  java.util.concurrent.atomic.AtomicBoolean hasCompleted
           
protected  boolean hasSlots
           
protected  java.util.concurrent.atomic.AtomicBoolean hasUserError
           
protected static java.util.concurrent.atomic.AtomicInteger nextGlobalID
           
protected  int relativeID
           
protected static int STARTED
           
protected  java.util.concurrent.atomic.AtomicInteger status
           
protected  TaskInfo taskInfo
           
 
Method Summary
 boolean cancelAttempt()
          Attempts to cancel the task.
protected  void cancelChildTasks()
          For internal use only.
 boolean cancelledSuccessfully()
          Checks to see if this task has successfully cancelled.
 boolean cancelRequested()
          Checks to see if this task has been requested to cancel.
protected  boolean currentThreadIsTheRegisteredThread()
           
protected  int executeIntermediateSlots()
           
protected  int executeSlots()
           
protected  int getCount()
           
 java.lang.Throwable getException()
          Returns the exception that occurred while this task executed.
protected  Slot getExceptionHandler(java.lang.Class occurredException)
           
 TaskIDGroup getGroup()
          Returns the group that this task is part of (assuming it is a multi-task).
 java.util.concurrent.BlockingQueue<E> getOutputQueue()
          If this is a pipeline stage, get a queue from which all future results can be retrieved.
protected  java.util.concurrent.BlockingQueue<E> getOutputQueue(TaskID requester)
          If this is a pipeline stage, get a queue from which all future results can be retrieved.
 int getProgress()
          Returns the current progress of this task
 E getReturnResult()
          Returns the result of the task.
 java.lang.Object[] getTaskArguments()
          Returns the arguments that were passed to this task when it was invoked.
 java.lang.String getTaskName()
          Returns the (method) name of the task.
 int globalID()
          Returns the task's globally-unique ID.
 boolean hasCompleted()
          Checks to see whether the task has completed.
 boolean hasUserError()
          Checks to see whether the task had any errors.
 boolean isInteractive()
          Checks to see if this task is an interactibe task.
 boolean isMultiTask()
          Checks to see if this task is part of a multi-task group.
 boolean isPipeline()
          Checks to see if this task if a part of a pipeline.
protected  boolean isSubTask()
           
 int multiTaskSize()
          Returns the size of the multi-task this task is part of.
 int relativeID()
          Returns the sub-task's relative ID in the multi-task.
protected  void setCount(int count)
           
protected  void setGlobalID(int globalID)
           
protected  void setPipelineThread(PipelineThread pt)
          Assigns the thread for this stage of the pipeline.
protected  void setSubTask(boolean isSubTask)
           
protected  void unregisterOutputQueue( queue)
          Unregisters a queue from a pipeline stage so that new results are no longer written to it.
 void waitTillFinished()
          Blocks the current thread until the task finishes.
protected  void writeToOutputQueues(E value)
          Write something into the output queue(s) handled by this TaskID.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

nextGlobalID

protected static java.util.concurrent.atomic.AtomicInteger nextGlobalID

globalID

protected int globalID

relativeID

protected int relativeID

enclosingTask

protected TaskID<?> enclosingTask

taskInfo

protected TaskInfo taskInfo

hasCompleted

protected java.util.concurrent.atomic.AtomicBoolean hasCompleted

cancelled

protected boolean cancelled

cancelRequested

protected java.util.concurrent.atomic.AtomicBoolean cancelRequested

hasUserError

protected java.util.concurrent.atomic.AtomicBoolean hasUserError

group

protected TaskIDGroup group

hasSlots

protected boolean hasSlots

CREATED

protected static final int CREATED
See Also:
Constant Field Values

CANCELLED

protected static final int CANCELLED
See Also:
Constant Field Values

STARTED

protected static final int STARTED
See Also:
Constant Field Values

status

protected java.util.concurrent.atomic.AtomicInteger status
Method Detail

getCount

protected int getCount()

setCount

protected void setCount(int count)

isSubTask

protected boolean isSubTask()

setSubTask

protected void setSubTask(boolean isSubTask)

cancelledSuccessfully

public boolean cancelledSuccessfully()
Checks to see if this task has successfully cancelled.

Returns:
true if it has cancelled successfully, false otherwise.

isInteractive

public boolean isInteractive()
Checks to see if this task is an interactibe task.

Returns:
true if this is an interactive task, false otherwise.

isPipeline

public boolean isPipeline()
Checks to see if this task if a part of a pipeline.


setPipelineThread

protected void setPipelineThread(PipelineThread pt)
Assigns the thread for this stage of the pipeline. Used for cancelling the stage via PipelineThread.cancel().


getOutputQueue

public java.util.concurrent.BlockingQueue<E> getOutputQueue()
If this is a pipeline stage, get a queue from which all future results can be retrieved. If not, returns null. Thread-safe.


getOutputQueue

protected java.util.concurrent.BlockingQueue<E> getOutputQueue(TaskID requester)
If this is a pipeline stage, get a queue from which all future results can be retrieved. If not, returns null. Intended for internal use only. Must pass the TaskID of the task requesting the queue in order to associate the queue with the tasks it connects. Thread-safe.


unregisterOutputQueue

protected void unregisterOutputQueue( queue)
Unregisters a queue from a pipeline stage so that new results are no longer written to it. Also, releases the reference so it can be GC'd. For internal use only. Thread-safe.


writeToOutputQueues

protected void writeToOutputQueues(E value)
Write something into the output queue(s) handled by this TaskID. Only existing queues will get the object. Thread-safe.


cancelChildTasks

protected void cancelChildTasks()
For internal use only. Cancel all child stages if this is a pipeline.


getProgress

public int getProgress()
Returns the current progress of this task

Returns:
The task's current progress.
See Also:
CurrentTask.getProgress(), CurrentTask.setProgress(int), setProgress(int)

cancelRequested

public boolean cancelRequested()
Checks to see if this task has been requested to cancel.

Returns:
true if it has been requested to cancel, false otherwise.
See Also:
CurrentTask.cancelRequested(), cancelAttempt(), cancelledSuccessfully()

cancelAttempt

public boolean cancelAttempt()
Attempts to cancel the task. If cancelled successfully, the task will not be enqueued. A failed cancel will still allow the task to continue executing. To stop the task, the task should check to see if a cancel request has been made.

Returns:
true if it has cancelled successfully, false otherwise.
See Also:
cancelRequested(), CurrentTask.cancelRequested(), cancelledSuccessfully()

getGroup

public TaskIDGroup getGroup()
Returns the group that this task is part of (assuming it is a multi-task).

Returns:
Returns the group associated with this task. If not part of a multi-task, then returns null.

getTaskName

public java.lang.String getTaskName()
Returns the (method) name of the task.

Returns:

globalID

public int globalID()
Returns the task's globally-unique ID.

Returns:
The task's unique ID.
See Also:
CurrentTask.globalID(), CurrentTask.relativeID(), relativeID()

relativeID

public int relativeID()
Returns the sub-task's relative ID in the multi-task.

Returns:
The position, starting from 0, of this sub-task compared to it's sibling subtasks.
See Also:
CurrentTask.globalID(), CurrentTask.relativeID(), globalID()

getReturnResult

public E getReturnResult()
                  throws java.util.concurrent.ExecutionException,
                         java.lang.InterruptedException
Returns the result of the task. If the task has not finished yet, the current thread blocks. ParaTask worker threads will not block, instead they execute other ready tasks until this task completes.

Returns:
The result of the task.
Throws:
java.util.concurrent.ExecutionException
java.lang.InterruptedException

waitTillFinished

public void waitTillFinished()
                      throws java.util.concurrent.ExecutionException,
                             java.lang.InterruptedException
Blocks the current thread until the task finishes. If the blocking thread is a ParaTask worker thread, then other tasks are executed until this task completes.

Throws:
java.util.concurrent.ExecutionException
java.lang.InterruptedException
Since:
25/05/2013 Before a worker tries to find another task and executes it, this thread should check if there is cancel request. If a worker thread is poisoned, even there are some unfinished children tasks, it will not get a chance to execute them, what it can do is check if they are finished or not(executed by other thread), 31/05/2013 Simplify the implementation of reducing thread number. Do not need to access the "PoisonPillBox" to get a "pill" Instead, using the class of "LottoBox"

currentThreadIsTheRegisteredThread

protected boolean currentThreadIsTheRegisteredThread()

getException

public java.lang.Throwable getException()
Returns the exception that occurred while this task executed.

Returns:
The exception that occurred.

hasCompleted

public boolean hasCompleted()
Checks to see whether the task has completed.

Returns:
true if it has completed, false otherwise
See Also:
getProgress(), CurrentTask.getProgress()

hasUserError

public boolean hasUserError()
Checks to see whether the task had any errors.

Returns:
true if there was an error, false otherwise

getExceptionHandler

protected Slot getExceptionHandler(java.lang.Class occurredException)

executeIntermediateSlots

protected int executeIntermediateSlots()

executeSlots

protected int executeSlots()

getTaskArguments

public java.lang.Object[] getTaskArguments()
Returns the arguments that were passed to this task when it was invoked.

Returns:
The arguments initially passed to the task when it was invoked

isMultiTask

public boolean isMultiTask()
Checks to see if this task is part of a multi-task group.

Returns:
true if this task is part of a multi-task, false otherwise

multiTaskSize

public int multiTaskSize()
Returns the size of the multi-task this task is part of.

Returns:
The multi-task size, otherwise returns 1 if this task is not part of a multi-task.

setGlobalID

protected void setGlobalID(int globalID)
Since:
: 10/05/2013 When multi task is expanded, call this method, and set global id for its sub tasks.