Package com.iizix

Class WorkerTask<TASK_ID,​PARAM,​RESULT>

  • Type Parameters:
    TASK_ID - The ID parameter type for the task.
    PARAM - The parameter type for the task.
    RESULT - The result type for the task.
    All Implemented Interfaces:
    java.lang.Comparable<java.lang.Object>

    public abstract class WorkerTask<TASK_ID,​PARAM,​RESULT>
    extends java.lang.Object
    implements java.lang.Comparable<java.lang.Object>
    The WorkerTask is something that requires processing in the Worker queue or some IO operations, etc.
    Author:
    Christopher Mindus
    • Nested Class Summary

      Nested Classes 
      Modifier and TypeClassDescription
      static class WorkerTask.State
      Task states.
    • Field Summary

      Fields 
      Modifier and TypeFieldDescription
      longcreateTime
      The time when the task was created.
      static intPRIORITY_DEFAULT
      The priority level of the task: default (5).
      static intPRIORITY_HIGHEST
      The priority level of the task: highest (10).
      static intPRIORITY_LOWEST
      The priority level of the task: lowest (0).
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and TypeMethodDescription
      booleancancel​(boolean interruptTaskThread)
      Cancels the task.
      intcompareTo​(java.lang.Object wt)
      Compares the priority level of the specified task with this one.
      voidexecute()
      Starts execution of the task.
      abstract RESULTexecute​(TASK_ID id, PARAM param)
      Start execution of the task with the implementation specific task ID and parameter.
      longgetEndTime()
      Gets the time in milliseconds (System.currentTimeMillis) when the task ended.
      java.lang.ThrowablegetException()
      Gets the exception thrown by the task if it completed with an exception.
      TASK_IDgetID()
      Gets the ID parameter.
      intgetModalID()
      Gets the modal ID when the modality extends beyond this process, e.g.
      PARAMgetParam()
      Gets the PARAM parameter.
      RESULTgetResult()
      Gets the result of the task if it has completed successfully.
      longgetStartTime()
      Gets the time in milliseconds (System.currentTimeMillis) when the task was started.
      WorkerTask.StategetState()
      Checks the current task state.
      WorkerNIOgetWorker()
      Gets the worker associated with this task.
      booleanisCanceled()
      Checks if the task is canceled.
      booleanisCancelled()
      Checks if the task is cancelled.
      voidsetCallback​(WorkerTaskEnded<TASK_ID,​PARAM,​RESULT> callback)
      Sets the callback function that is called upon end of the task, called regardless if a result is set for normal completion, or an exception.
      voidwaitForTaskEnded()
      Waits for task to end.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • PRIORITY_LOWEST

        public static final int PRIORITY_LOWEST
        The priority level of the task: lowest (0).
        See Also:
        Constant Field Values
      • PRIORITY_DEFAULT

        public static final int PRIORITY_DEFAULT
        The priority level of the task: default (5).
        See Also:
        Constant Field Values
      • PRIORITY_HIGHEST

        public static final int PRIORITY_HIGHEST
        The priority level of the task: highest (10).
        See Also:
        Constant Field Values
      • createTime

        public final long createTime
        The time when the task was created.
    • Constructor Detail

      • WorkerTask

        public WorkerTask()
        Constructor for the task to perform, no task ID, no parameter, no callback, default priority.
      • WorkerTask

        public WorkerTask​(PARAM param)
        Constructor for the task to perform, no task ID, no callback, default priority.
        Parameters:
        param - The implementation specific parameter, or null for none.
      • WorkerTask

        public WorkerTask​(TASK_ID id,
                          PARAM param)
        Constructor for the task to perform, no callback, default priority.
        Parameters:
        id - The implementation specific task ID.
        param - The implementation specific parameter, or null for none.
      • WorkerTask

        public WorkerTask​(TASK_ID id,
                          PARAM param,
                          WorkerTaskEnded<TASK_ID,​PARAM,​RESULT> callback)
        Constructor for the task to perform, default priority.
        Parameters:
        id - The implementation specific task ID.
        param - The implementation specific parameter, or null for none.
        callback - The callback function to call when task ends or is cancelled, null for none.
      • WorkerTask

        public WorkerTask​(TASK_ID id,
                          PARAM param,
                          WorkerTaskEnded<TASK_ID,​PARAM,​RESULT> callback,
                          int priority)
        Constructor for the task to perform.
        Parameters:
        id - The implementation specific task ID.
        param - The implementation specific parameter, or null for none.
        callback - The callback function to call when task ends or is cancelled, null for none.
        priority - The priority of the task.
    • Method Detail

      • setCallback

        public final void setCallback​(WorkerTaskEnded<TASK_ID,​PARAM,​RESULT> callback)
        Sets the callback function that is called upon end of the task, called regardless if a result is set for normal completion, or an exception.
        Parameters:
        callback - The callback function to call when task ends or is cancelled, null for none.
      • execute

        public final void execute()
        Starts execution of the task. This method is called by the invoker of the task, i.e. the Worker.
      • execute

        public abstract RESULT execute​(TASK_ID id,
                                       PARAM param)
                                throws java.lang.Exception
        Start execution of the task with the implementation specific task ID and parameter.
        Parameters:
        id - The implementation specific task ID.
        param - The implementation specific parameter, or null for none.
        Returns:
        The return code.
        Throws:
        java.lang.Exception - If some error occurs during processing of the task.
      • getModalID

        public final int getModalID()
        Gets the modal ID when the modality extends beyond this process, e.g. from Server to Client, where Client will reply later to Server.
        Returns:
        The modal ID, zero indicates no modality for the task.
      • getState

        public final WorkerTask.State getState()
        Checks the current task state.
        Returns:
        The state: NOT_STARTED, RUNNING, COMPLETED, EXCEPTION.
      • getID

        public final TASK_ID getID()
        Gets the ID parameter.
        Returns:
        The TASK_ID.
      • getParam

        public final PARAM getParam()
        Gets the PARAM parameter.
        Returns:
        The PARAM.
      • getResult

        public final RESULT getResult()
        Gets the result of the task if it has completed successfully.
        Returns:
        The return result from the task, or null if the task has not yet or didn't complete normally.
      • getException

        public final java.lang.Throwable getException()
        Gets the exception thrown by the task if it completed with an exception.
        Returns:
        The exception thrown by the task, or null if it completed normally or has not yet completed.
      • isCanceled

        public final boolean isCanceled()
        Checks if the task is canceled.
        Returns:
        true if canceled, false otherwise.
      • isCancelled

        public final boolean isCancelled()
        Checks if the task is cancelled.
        Returns:
        true if cancelled, false otherwise.
      • cancel

        public boolean cancel​(boolean interruptTaskThread)
        Cancels the task.
        Parameters:
        interruptTaskThread - Flag indicating if the task thread should be interrupted (if running) with call to thread.interrupt();.
        Returns:
        true for success, false for failure.
      • waitForTaskEnded

        public void waitForTaskEnded()
                              throws java.lang.InterruptedException
        Waits for task to end. If the task hasn't been started yet, it will wait for it to end or to be cancelled.
        Throws:
        java.lang.InterruptedException - If the current thread is interrupted during the waiting.
      • getStartTime

        public long getStartTime()
        Gets the time in milliseconds (System.currentTimeMillis) when the task was started.
        Returns:
        zero if the has has not yet been started.
      • getEndTime

        public long getEndTime()
        Gets the time in milliseconds (System.currentTimeMillis) when the task ended.
        Returns:
        zero if the has has not yet ended.
      • getWorker

        public final WorkerNIO getWorker()
        Gets the worker associated with this task.
        Returns:
        The worker for the task, null if the task has not yet been added to the worker queue.
      • compareTo

        public final int compareTo​(java.lang.Object wt)
        Compares the priority level of the specified task with this one.
        Specified by:
        compareTo in interface java.lang.Comparable<TASK_ID>
        Parameters:
        wt - The worker task to compare priority with.
        Returns:
        -1, 0 or 1 for the priority level.