pt.runtime
Class CurrentTask

java.lang.Object
  extended by pt.runtime.CurrentTask

public class CurrentTask
extends java.lang.Object

Helper methods for the currently executing task. This class contains various functions in regards to the current task.

The methods in this class are only applicable to the currently executed task, and will therefore throw a RuntimeException when called from a non-task. To inquire if within a task, programmers may use insideTask().

Author:
Nasser Giacaman, Oliver Sinnen

Method Summary
static void barrier()
          Barrier synchronisation for multi-tasks.
static boolean cancelRequested()
          The current task checks to see if it has been requested to cancel.
static TaskID currentTaskID()
          Return the current TaskID associated with the currently executing task.
static int currentThreadID()
          Returns the ParaTask thread's ID.
static int currentThreadLocalID()
           
static int getProgress()
          Returns the progress of the currently executed task.
static int globalID()
          Returns the current task's global ID.
static boolean insideTask()
          Inquire as to whether the current code being executed is inside a task.
static boolean isMultiTask()
           
static boolean isOneoffTask()
           
static boolean isSubTask()
           
static int multiTaskSize()
          Returns the current task's multi-task size.
static
<E> void
publishInterim(E interimResult)
          Publish intermediate results.
static int relativeID()
          All tasks have a relative ID.
static void setProgress(int progress)
          Updates the progress of the currently executing task.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

currentThreadID

public static int currentThreadID()
Returns the ParaTask thread's ID. This method is only applicable for ParaTask threads, and must therefore only be called within ParaTask tasks.

Returns:
The ParaTask thread's ID.
Throws:
java.lang.RuntimeException - if not called from within a ParaTask task.

currentThreadLocalID

public static int currentThreadLocalID()
Returns:
The ParaTask thread's Local ID.
Throws:
java.lang.RuntimeException - if not called from within a ParaTask task.
Since:
10/05/2013 Returns the ParaTask thread's Local ID. This method is only applicable for ParaTask threads, and must therefore only be called within ParaTask tasks.

insideTask

public static boolean insideTask()
Inquire as to whether the current code being executed is inside a task. Useful in some applications where the same code fragment might be executed both as a task and sequentially. For example:

 
 TASK int computeTask(int value) {
                return compute(value); 
 }
 
 public int compute(int value) {
                ...
                if (ParaTask.insideTask()) {
                        // this method is being executed as a ParaTask task
                } else {
                        // this method is not being executed as a ParaTask task
                        // (maybe the event dispatch thread, main thread, or another user-defined thread)   
                }
                ...
 }
 
 

Returns:
true if the currently inside a task, false otherwise.

multiTaskSize

public static int multiTaskSize()
Returns the current task's multi-task size. i.e. the number of sub-tasks for the current sub-task's multi-task. If the current task is not part of a multi-task, then 1 is always returned.

Returns:
The size of the current task's group.
Throws:
java.lang.RuntimeException - if not called from within a ParaTask task.

getProgress

public static int getProgress()
Returns the progress of the currently executed task.

Returns:
The progress, as updated by the task.
Throws:
java.lang.RuntimeException - if not called from within a ParaTask task.
See Also:
TaskID.setProgress(int), setProgress(int)

setProgress

public static void setProgress(int progress)
Updates the progress of the currently executing task.

Parameters:
progress - The new progress of the currently executing task.
Throws:
java.lang.RuntimeException - if not called from within a ParaTask task.
See Also:
TaskID.getProgress(), getProgress()

currentTaskID

public static TaskID currentTaskID()
Return the current TaskID associated with the currently executing task.

Returns:
The current task's TaskID
Throws:
java.lang.RuntimeException - if not called from within a ParaTask task.

globalID

public static int globalID()
Returns the current task's global ID. All tasks have a unique ID, and this is it.

Returns:
The current task's globally-unique ID
Throws:
java.lang.RuntimeException - if not called from within a ParaTask task.
See Also:
TaskID.globalID()

relativeID

public static int relativeID()
All tasks have a relative ID. But this only makes sense for multi-tasks. If the current task is not part of a multi-task, then this always returns 0. Relative IDs start from 0.

Returns:
The current task's relative ID
Throws:
java.lang.RuntimeException - if not called from within a ParaTask task.
See Also:
TaskID.relativeID()

cancelRequested

public static boolean cancelRequested()
The current task checks to see if it has been requested to cancel.

Returns:
true if the current task has been asked to cancel, false otherwise.
Throws:
java.lang.RuntimeException - if not called from within a ParaTask task.
See Also:
TaskID.cancelAttempt(), TaskID.cancelRequested()

publishInterim

public static <E> void publishInterim(E interimResult)
Publish intermediate results. If any slots are registered to listen for interim results (i.e. using either the notifyInterim or notifyInterimGUI clause), this is the method to publish those results to them.

Type Parameters:
E - The type of the interim result to be published
Parameters:
interimResult - The interim result being published.
Throws:
java.lang.RuntimeException - if not called from within a ParaTask task.

barrier

public static void barrier()
                    throws java.lang.InterruptedException,
                           java.util.concurrent.BrokenBarrierException
Barrier synchronisation for multi-tasks. To avoid deadlock, it is important that all sub-tasks of the multi-task call this method, otherwise sub-tasks will continue to wait and never complete! This is safe to call recursively and by multiple multi-tasks, since a "waiting" worker thread will in fact execute other ready tasks while it waits for the sibling sub-tasks to also reach the barrier.

Throws:
java.lang.InterruptedException
java.util.concurrent.BrokenBarrierException
java.lang.RuntimeException - if not called from within a ParaTask task.

isOneoffTask

public static boolean isOneoffTask()
Returns:
true, if current task is one-off task. Otherwise false Check if current executing task is one-off task. If current thread is one-off task thread, its current task is definitely one-off task.
Since:
21/05/2013

isMultiTask

public static boolean isMultiTask()
Returns:
true, if current task is multi task. Otherwise false Check if current executing task is multi task. If current thread is multi task thread, its current task is definitely multi task.
Since:
21/05/2013

isSubTask

public static boolean isSubTask()
Returns:
true, if current task is sub task. Otherwise false Check if current executing task is sub task.
Since:
21/05/2013