Package com.iizix
Class WorkerTask<TASK_ID,PARAM,RESULT>  
java.lang.Object
com.iizix.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:
- Comparable<Object>
The WorkerTask is something that requires processing in the Worker queue or some IO operations, etc.
- Author:
- Christopher Mindus
- Nested Class SummaryNested Classes
- Field SummaryFieldsModifier and TypeFieldDescription- final longThe time when the task was created.- static final intThe priority level of the task: default (5).- static final intThe priority level of the task: highest (10).- static final intThe priority level of the task: lowest (0).
- Constructor SummaryConstructorsConstructorDescriptionConstructor for the task to perform, no task ID, no parameter, no callback, default priority.- WorkerTask- (PARAM param) Constructor for the task to perform, no task ID, no callback, default priority.- WorkerTask- (TASK_ID id, PARAM param) Constructor for the task to perform, no callback, default priority.- WorkerTask- (TASK_ID id, PARAM param, WorkerTaskEnded<TASK_ID, - PARAM, - RESULT> callback) Constructor for the task to perform, default priority.- WorkerTask- (TASK_ID id, PARAM param, WorkerTaskEnded<TASK_ID, - PARAM, - RESULT> callback, int priority) Constructor for the task to perform.
- Method SummaryModifier and TypeMethodDescription- boolean- cancel- (boolean interruptTaskThread) Cancels the task.- final intCompares the priority level of the specified task with this one.- final void- execute()Starts execution of the task.- abstract RESULTStart execution of the task with the implementation specific task ID and parameter.- longGets the time in milliseconds (System.currentTimeMillis) when the task ended.- final ThrowableGets the exception thrown by the task if it completed with an exception.- final TASK_ID- getID()Gets the ID parameter.- final intGets the modal ID when the modality extends beyond this process, e.g.- final PARAM- getParam()Gets the- PARAMparameter.- final RESULTGets the result of the task if it has completed successfully.- longGets the time in milliseconds (System.currentTimeMillis) when the task was started.- final WorkerTask.State- getState()Checks the current task state.- final WorkerNIOGets the worker associated with this task.- final booleanChecks if the task is canceled.- final booleanChecks if the task is cancelled.- 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.- voidWaits for task to end.
- Field Details- PRIORITY_LOWESTpublic static final int PRIORITY_LOWESTThe priority level of the task: lowest (0).- See Also:
 
- PRIORITY_DEFAULTpublic static final int PRIORITY_DEFAULTThe priority level of the task: default (5).- See Also:
 
- PRIORITY_HIGHESTpublic static final int PRIORITY_HIGHESTThe priority level of the task: highest (10).- See Also:
 
- createTimepublic final long createTimeThe time when the task was created.
 
- Constructor Details- WorkerTaskpublic WorkerTask()Constructor for the task to perform, no task ID, no parameter, no callback, default priority.
- WorkerTaskConstructor for the task to perform, no task ID, no callback, default priority.- Parameters:
- param- The implementation specific parameter, or null for none.
 
- WorkerTaskConstructor 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.
 
- WorkerTaskConstructor 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.
 
- WorkerTaskpublic 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 Details- setCallbackSets 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.
 
- executepublic final void execute()Starts execution of the task. This method is called by the invoker of the task, i.e. the Worker.
- executeStart 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:
- Exception- If some error occurs during processing of the task.
 
- getModalIDpublic 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.
 
- getStateChecks the current task state.- Returns:
- The state: NOT_STARTED, RUNNING, COMPLETED, EXCEPTION.
 
- getIDGets the ID parameter.- Returns:
- The TASK_ID.
 
- getParamGets the- PARAMparameter.- Returns:
- The PARAM.
 
- getResultGets 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.
 
- getExceptionGets 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.
 
- isCanceledpublic final boolean isCanceled()Checks if the task is canceled.- Returns:
- true if canceled, false otherwise.
 
- isCancelledpublic final boolean isCancelled()Checks if the task is cancelled.- Returns:
- true if cancelled, false otherwise.
 
- cancelpublic 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.
 
- waitForTaskEndedWaits for task to end. If the task hasn't been started yet, it will wait for it to end or to be cancelled.- Throws:
- InterruptedException- If the current thread is interrupted during the waiting.
 
- getStartTimepublic long getStartTime()Gets the time in milliseconds (System.currentTimeMillis) when the task was started.- Returns:
- zero if the has has not yet been started.
 
- getEndTimepublic long getEndTime()Gets the time in milliseconds (System.currentTimeMillis) when the task ended.- Returns:
- zero if the has has not yet ended.
 
- getWorkerGets 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.
 
- compareToCompares the priority level of the specified task with this one.- Specified by:
- compareToin interface- Comparable<TASK_ID>
- Parameters:
- wt- The worker task to compare priority with.
- Returns:
- -1, 0 or 1 for the priority level.