Class AbstractTaskExecutor
- All Implemented Interfaces:
Executor,TaskExecutor
- Direct Known Subclasses:
AbstractTaskExecutorService,ManualTaskExecutor
TaskExecutor implementations.
AbstractTaskExecutor defines default implementations for all
the execute methods which all rely on the protected
submitTask
method. Only this submitTask method is needed to be implemented by
subclasses to actually schedule a task. Note that all the execute
methods rely directly the submitTask method and
overriding any of them has no effect on the others (i.e.: they don't call
each other). For further details on how to implement the submitTask
method: see its documentation.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classDefines the submitted task to be executed by subclasses ofAbstractTaskExecutor. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<V> CompletionStage<V>executeFunction(CancellationToken cancelToken, CancelableFunction<? extends V> function) Executes the function at some time in the future.protected abstract voidsubmitTask(CancellationToken cancelToken, AbstractTaskExecutor.SubmittedTask<?> submittedTask) Implementations must override this method to actually execute submitted tasks.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.jtrim2.executor.TaskExecutor
execute, execute, executeStaged
-
Constructor Details
-
AbstractTaskExecutor
public AbstractTaskExecutor()
-
-
Method Details
-
submitTask
protected abstract void submitTask(CancellationToken cancelToken, AbstractTaskExecutor.SubmittedTask<?> submittedTask) Implementations must override this method to actually execute submitted tasks.Assuming no cancellation requests, implementations must call
submittedTask.execute.Cancellation requests can be detected using the provided
CancellationTokenand if an implementation chooses not to even try to executetask, it must only callsubmittedTask.cancel.Implementations must always complete the passed task in some way. Either by calling its
executeor itscompleteExceptionally(orcancel) method.- Parameters:
cancelToken- theCancellationTokenwhich can be checked by implementations if the currently submitted task has been canceled. Also this is theCancellationTokenimplementations should pass totask. This argument cannot benull.submittedTask- the task to be executed. Implementations must execute this task at most once by calling itsexecutemethod. If the execute method is not called, an exceptional return state must be set via thecompleteExceptionallymethod. This argument cannot benull.
-
executeFunction
public <V> CompletionStage<V> executeFunction(CancellationToken cancelToken, CancelableFunction<? extends V> function) Executes the function at some time in the future. When and on what thread, the function is to be executed is completely implementation dependent. Implementations may choose to execute tasks later on a separate thread or synchronously in the calling thread at the discretion of the implementation.- Specified by:
executeFunctionin interfaceTaskExecutor- Type Parameters:
V- the type of the result of the submitted function- Parameters:
cancelToken- theCancellationTokenwhich is to be checked if the submitted task is to be canceled. If thisCancellationTokensignals a cancellation request, thisTaskExecutormay choose to not even attempt to execute the submitted task. This argument may not benull. When the task cannot be canceled, use the staticCancellation.UNCANCELABLE_TOKENfor this argument (even in this case, theTaskExecutormay be able to cancel the task, if it was not submitted for execution).function- the function to be executed by thisTaskExecutor. This argument cannot benull.- Returns:
- the
CompletionStagewhich can be used to execute tasks after the completion of the submitted function and process the result of the submitted function. This method never returnsnull. - See Also:
-