Package org.jtrim2.executor
Interface TaskExecutor
- All Superinterfaces:
Executor
- All Known Subinterfaces:
ContextAwareTaskExecutor,ContextAwareTaskExecutorService,MonitorableTaskExecutor,MonitorableTaskExecutorService,TaskExecutorService
- All Known Implementing Classes:
AbstractTaskExecutor,AbstractTaskExecutorService,AbstractTerminateNotifierTaskExecutorService,ContextAwareWrapper,DelegatedTaskExecutorService,ManualTaskExecutor,SingleThreadedExecutor,SyncTaskExecutor,ThreadPoolTaskExecutor
Executes tasks at some time in the future. This interface defines a more
robust way to execute tasks than
java.util.concurrent.Executor.
That is, this interface defines a simpler way for canceling tasks and allows
to continue after the completion of the submitted task via a CompletionStage.
It is possible to continue execution regardless how the submitted task completed
(success, failure or canceled).
For more control over the life of a TaskExecutor, see the extending
TaskExecutorService interface.
Thread safety
Implementations of this interface are required to be safely accessible from multiple threads concurrently.Synchronization transparency
The methods of this interface are not required to be synchronization transparent because they may execute tasks, handlers added toCompletionStage, etc.-
Method Summary
Modifier and TypeMethodDescriptiondefault voiddefault CompletionStage<Void>execute(CancellationToken cancelToken, CancelableTask task) Executes the task at some time in the future.<V> CompletionStage<V>executeFunction(CancellationToken cancelToken, CancelableFunction<? extends V> function) Executes the function at some time in the future.default CompletionStage<Void>executeStaged(Runnable task) Executes an non-cancelable task at some time in the future.
-
Method Details
-
executeFunction
<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.- 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. - Throws:
NullPointerException- thrown if theCancellationTokenor the task isnull- See Also:
-
execute
Executes the task at some time in the future. When and on what thread, the task 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.- 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).task- the task to be executed by thisTaskExecutor. This argument cannot benull.- Returns:
- the
CompletionStagewhich can be used to execute tasks after the completion of the submitted task. This method never returnsnull. - Throws:
NullPointerException- thrown if theCancellationTokenor the task isnull- See Also:
-
executeStaged
Executes an non-cancelable task at some time in the future. When and on what thread, the task 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.Note that although the task itself is not cancelable explicitly, the executor might cancel it by not executing the submitted task. For example, due to a call to the
shutdownAndCancelmethod ofTaskExecutorService.The default implementation delegates the call to
execute(CancellationToken, CancelableTask).- Parameters:
task- the task to be executed by thisTaskExecutor. This argument cannot benull.- Returns:
- the
CompletionStagewhich can be used to execute tasks after the completion of the submitted task. This method never returnsnull. - Throws:
NullPointerException- thrown if the task isnull- See Also:
-
execute
Note that although the task itself is not cancelable explicitly, the executor might cancel it by not executing the submitted task. For example, due to a call to the
shutdownAndCancelmethod ofTaskExecutorService.The default implementation delegates the call to
executeStagedand logs uncaught errors thrown by the submitted task.
-