Class ThreadTask


  • public class ThreadTask
    extends java.lang.Object
    A task that facilitates the use of a hierarchic tree of tasks and subtasks.

    Often parallel processing can be done at multiple levels. Typically it is not known
    beforehand if some task can be split into multiple smaller subtasks or not. The ThreadTask
    keeps track of the state of all its subtasks - only when all subtasks have been processed,
    the parent task will be able to finish. Instead of finishing, it can also choose to add new
    subtasks that depends on the result of the previously run subtasks.

    The ThreadTask can utilize a ThreadPool of arbitrary size (down to 0 threads).
    When 0 threads are used, the addSubTask() function will be blocking and execute the work
    immediately. If more than 0 threads are used, the addSubTask function will return
    immediately, and the task is instead added to the work queue for processing when a thread
    becomes available.

    There are two ways to use the ThreadTask:

    - Use it as a grouping mechanism: here one or more subtasks can be added for parallel
    processing. One can use the ThreadPool size to adjust the number of threads one wants to use
    for a specific application.

    - Subclass it and override the four standard functions to make more complex
    "branch-and-combine-results" type of tasks.

    The four standard functions are as follows:

    run() is the main work unit of the ThreadTask.

    subTaskDone() is called each time a subtask has ended.

    idle() is called when the task has finished its run() function and all subtasks has ended.

    done() is the final function called before the ThreadTask is ended completely.

    Please remember that the four functions can in general not be expected to be run in the same
    thread! In the first three functions it is allowed to add new subtasks to keep the thread
    running. In the done() function this is not allowed (it is simply ignored as the task will
    end immediately after this function has been called).
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  ThreadTask.TaskState
      The different execution states of a task.
    • Constructor Summary

      Constructors 
      Constructor Description
      ThreadTask()
      Create task that will use a specific thread pool.
      If no thread pool is given, the task will not use parallelization.
      ThreadTask​(long cPtr, boolean cMemoryOwn)  
      ThreadTask​(ThreadPoolPtr pool)
      Create task that will use a specific thread pool.
      If no thread pool is given, the task will not use parallelization.
      ThreadTask​(ThreadTaskPtr parent)
      Create task that will inherit parents thread pool.

    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean addSubTask​(ThreadTaskPtr subtask)
      Add a child task to this task.

      This task will not end before all child tasks has ended.
      Never call execute on the child tasks - they will be executed by the parent task.

      void delete()  
      void done()
      Function is executed when work is finished (at this point new subtasks can NOT be
      added).
      boolean execute()
      Start executing the work in this task and all subtasks already added, by using the
      assigned ThreadPool.
      Note: There is no guarantee that the parent run() function starts executing before the
      childrens run() functions.
      static long getCPtr​(ThreadTask obj)  
      SWIGTYPE_p_std__vectorT_rw__core__Exception_t getException()
      Get a list of exceptions registered in task and subtasks.
      ThreadTask.TaskState getState()
      Get the current state of the task (non-blocking).
      SWIGTYPE_p_std__vectorT_rw__core__PtrT_rw__common__ThreadTask_t_t getSubTasks()
      Get the subtasks currently added to the ThreadTask.
      ThreadPoolPtr getThreadPool()
      Get the ThreadPool that is used by this task currently.

      void idle()
      Function is executed when the task becomes idle (new subtasks can be added in
      this function).
      boolean keepAlive()
      Check is the task has keep alive option enabled.
      void registerFailure​(RWException e)
      Mark the task as a failure by registering an exception.

      The user should override the #subTaskDone function in the parent task to
      either handle the exceptions appropriately or propagate them further on.

      void run()
      Function is the first function executed to do the actual work (new subtasks can
      be added in this function).
      void setKeepAlive​(boolean enable)
      Choose if the thread should exit automatically when all work and children has
      finished (this is the default).

      Remember to set this to false when there is no more work to be done by the task, to allow
      the task to stop.

      boolean setThreadPool​(ThreadPoolPtr pool)
      Set which ThreadPool to use to do the actual execution of work.
      When execution is started the pool can not be changed anymore.
      void subTaskDone​(ThreadTask subtask)
      Function is executed each time a subtask has finished (new subtasks can be added
      in this function).
      ThreadTask.TaskState wait​(ThreadTask.TaskState previous)
      Wait until state of task changes (blocking).

      Remember to check if the new State is the desired (for instance DONE).

      void waitUntilDone()
      Wait until state of task changes to DONE (blocking).
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ThreadTask

        public ThreadTask​(long cPtr,
                          boolean cMemoryOwn)
      • ThreadTask

        public ThreadTask​(ThreadTaskPtr parent)
        Create task that will inherit parents thread pool.

        Parameters:
        parent - [in] the parent task to take the thread pool from.
      • ThreadTask

        public ThreadTask​(ThreadPoolPtr pool)
        Create task that will use a specific thread pool.
        If no thread pool is given, the task will not use parallelization.
        Parameters:
        pool - [in] (optional) a pointer to the ThreadPool to use.
      • ThreadTask

        public ThreadTask()
        Create task that will use a specific thread pool.
        If no thread pool is given, the task will not use parallelization.
    • Method Detail

      • getCPtr

        public static long getCPtr​(ThreadTask obj)
      • delete

        public void delete()
      • setThreadPool

        public boolean setThreadPool​(ThreadPoolPtr pool)
        Set which ThreadPool to use to do the actual execution of work.
        When execution is started the pool can not be changed anymore.
        Parameters:
        pool - [in] pointer to the pool
        Returns:
        true if change was successful, false otherwise.
      • getThreadPool

        public ThreadPoolPtr getThreadPool()
        Get the ThreadPool that is used by this task currently.

        Returns:
        pointer to the ThreadPool.
      • run

        public void run()
        Function is the first function executed to do the actual work (new subtasks can
        be added in this function).
      • subTaskDone

        public void subTaskDone​(ThreadTask subtask)
        Function is executed each time a subtask has finished (new subtasks can be added
        in this function). If #registerFailure is used to register failures in subtasks, this
        function should handle or propagate the failures. The default implementation of this
        function is as follows:
        for(const Exception& e : subtask->getExceptions()) { registerFailure(e); }

        Parameters:
        subtask - [in] the subtask that just finished.
      • idle

        public void idle()
        Function is executed when the task becomes idle (new subtasks can be added in
        this function).
      • done

        public void done()
        Function is executed when work is finished (at this point new subtasks can NOT be
        added).
      • execute

        public boolean execute()
        Start executing the work in this task and all subtasks already added, by using the
        assigned ThreadPool.
        Note: There is no guarantee that the parent run() function starts executing before the
        childrens run() functions.
        Returns:
        true if execution started successfully, false if already running or thread has
        finished.
      • wait

        public ThreadTask.TaskState wait​(ThreadTask.TaskState previous)
        Wait until state of task changes (blocking).

        Remember to check if the new State is the desired (for instance DONE).

        Parameters:
        previous - [in] the previous state (wait for changes from this)
        Returns:
        the new TaskState
      • waitUntilDone

        public void waitUntilDone()
        Wait until state of task changes to DONE (blocking).
      • getState

        public ThreadTask.TaskState getState()
        Get the current state of the task (non-blocking).
        Returns:
        the current TaskState.
      • addSubTask

        public boolean addSubTask​(ThreadTaskPtr subtask)
        Add a child task to this task.

        This task will not end before all child tasks has ended.
        Never call execute on the child tasks - they will be executed by the parent task.

        Parameters:
        subtask - the ThreadTask to add as child.
        Returns:
        true if child was added successfully (only if task has not already ended)
      • setKeepAlive

        public void setKeepAlive​(boolean enable)
        Choose if the thread should exit automatically when all work and children has
        finished (this is the default).

        Remember to set this to false when there is no more work to be done by the task, to allow
        the task to stop.

        Parameters:
        enable - [in] true if the thread should NOT finish automatically.
      • keepAlive

        public boolean keepAlive()
        Check is the task has keep alive option enabled.
        Returns:
        true if task should be kept alive, false otherwise.
      • registerFailure

        public void registerFailure​(RWException e)
        Mark the task as a failure by registering an exception.

        The user should override the #subTaskDone function in the parent task to
        either handle the exceptions appropriately or propagate them further on.

        Parameters:
        e - [in] an exception describing the problem.