Interface TaskExecutorService
- All Superinterfaces:
Executor,TaskExecutor
- All Known Subinterfaces:
ContextAwareTaskExecutorService,MonitorableTaskExecutorService
- All Known Implementing Classes:
AbstractTaskExecutorService,AbstractTerminateNotifierTaskExecutorService,DelegatedTaskExecutorService,SingleThreadedExecutor,SyncTaskExecutor,ThreadPoolTaskExecutor
TaskExecutor with additional methods to better manage
submitted tasks. This interface is a replacement for the
ExecutorService interface in java.
In general, TaskExecutorService implementations must be shut down
once no longer needed, so that implementations may terminate their internal
threads. When a TaskExecutorService has been shut down it will no
longer accept submitting tasks and as a consequence, it will complete them
exceptionally with an OperationCanceledException.
TaskExecutorService defines two ways for shutting down itself:
One is the shutdown() method and the other is the
shutdownAndCancel() method. The difference
between them is that while shutdown() only prevents submitting
subsequent tasks, shutdownAndCancel() will actively cancel already
submitted tasks.
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.- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionaddTerminateListener(Runnable listener) Adds a listener which is to be notified after thisTaskExecutorServiceterminates.default voidawaitTermination(CancellationToken cancelToken) Waits until thisTaskExecutorServicewill not execute any more tasks.booleanChecks whether thisTaskExecutorServiceaccepts newly submitted tasks or not.booleanChecks whether thisTaskExecutorServicemay execute tasks submitted to it tasks or not.voidshutdown()Shuts down thisTaskExecutorService, so that it will not execute tasks submitted to it after this method call returns.voidShuts down thisTaskExecutorServiceand cancels already submitted tasks, so that it will not execute tasks submitted to it after this method call returns.booleantryAwaitTermination(CancellationToken cancelToken, long timeout, TimeUnit unit) Waits until thisTaskExecutorServicewill not execute any more tasks or the given timeout elapses.Methods inherited from interface org.jtrim2.executor.TaskExecutor
execute, execute, executeFunction, executeStaged
-
Method Details
-
shutdown
void shutdown()Shuts down thisTaskExecutorService, so that it will not execute tasks submitted to it after this method call returns.Already submitted tasks will execute normally but tasks submitted after this method returns will immediately be completed exceptionally with an
OperationCanceledException.Note that it is possible, that some tasks are submitted concurrently with this call. Those tasks can be either canceled or executed normally, depending on the circumstances.
If currently executing tasks should be canceled as well, use the
shutdownAndCancel()method to shutdown thisTaskExecutorService.This method call is idempotent. That is, calling it multiple times must have no further effect.
- See Also:
-
shutdownAndCancel
void shutdownAndCancel()Shuts down thisTaskExecutorServiceand cancels already submitted tasks, so that it will not execute tasks submitted to it after this method call returns.Already submitted tasks will be canceled and the tasks may detect this cancellation request by inspecting their
CancellationTokenbut tasks submitted after this method returns will immediately be completed exceptionally with anOperationCanceledException.Note that it is possible, that some tasks are submitted concurrently with this call. Those tasks may be treated as if they were submitted before this method call or as if they were submitted after.
If currently executing tasks should be left executing, use the
shutdown()method instead to shutdown thisTaskExecutorService.This method call is idempotent. That is, calling it multiple times must have no further effect. Note however, that calling this method after the
shutdown()method is meaningful because this method will cancel ongoing tasks.- See Also:
-
isShutdown
boolean isShutdown()Checks whether thisTaskExecutorServiceaccepts newly submitted tasks or not. This method returnstrue, if and only, if either the#shutdown() shutdown()or theshutdownAndCancel()method has been called. Therefore if, this method returnstrue, subsequentsubmitandexecutemethod invocations will not execute submitted tasks and will only complete them exceptionally with anOperationCanceledException.- Returns:
trueif thisTaskExecutorServiceaccepts newly submitted tasks,falseif it has been shut down- See Also:
-
isTerminated
boolean isTerminated()Checks whether thisTaskExecutorServicemay execute tasks submitted to it tasks or not. If this method returnstrue, no more tasks will be executed by thisTaskExecutorServiceand no tasks are currently executing. That is, if this method returnstruesubsequentsubmitorexecutemethods will not execute the submitted tasks but complete them exceptionally with anOperationCanceledException.Also if this method returns
true, subsequentawaitTerminationmethod calls will return immediately without throwing an exception (will not even check for cancellation).- Returns:
trueif thisTaskExecutorServiceaccepts newly submitted tasks,falseif it has been shut down- See Also:
-
addTerminateListener
Adds a listener which is to be notified after thisTaskExecutorServiceterminates. If the listener has been already terminated, the listener is notified immediately in thisaddTerminateListenermethod call. Also, the listener may only be called at most once (per registration).Whenever these registered listeners are notified, the
isTerminated()method already returnstrueand calling theawaitTerminationor thetryAwaitTerminationmethod will have no effect (as they return immediately).On what thread, the registered listeners might be called is implementation dependent: They can be called from the thread, the last task executed, the
shutdownor theshutdownAndCancelmethods or in any other thread, as thisTaskExecutorServicedesires. Therefore, the listeners must be written conservatively.The listener can be removed if no longer required to be notified by calling the
unregistermethod of the returned reference.- Parameters:
listener- theRunnablewhoserunmethod is to be called after thisTaskExecutorServiceterminates. This argument cannot benull.- Returns:
- the reference which can be used to removed the currently added
listener, so that it will not be notified anymore. This method never
returns
null. - Throws:
NullPointerException- thrown if the specified listener isnull- See Also:
-
awaitTermination
Waits until thisTaskExecutorServicewill not execute any more tasks. After this method returns (without throwing an exception), subsequentisTerminated()method calls will returntrue.After this method returns without throwing an exception, it is true that: No more tasks will be executed by this
TaskExecutorServiceand no tasks are currently executing. That is, subsequentsubmitorexecutemethods will not execute the submitted tasks but complete them exceptionally with anOperationCanceledException.The default implementation simply calls
tryAwaitTerminationuntil it returnstrue.- Parameters:
cancelToken- theCancellationTokenwhich can be used to stop waiting for the termination of thisTaskExecutorService. That is, if this method detects, that cancellation was requested, it will throw anOperationCanceledException. This argument cannot benull.- Throws:
NullPointerException- thrown if the specifiedCancellationTokenisnullOperationCanceledException- thrown if cancellation request was detected by this method before thisTaskExecutorServiceterminated. This exception is not thrown if thisTaskExecutorServicewas terminated prior to this method call.- See Also:
-
tryAwaitTermination
Waits until thisTaskExecutorServicewill not execute any more tasks or the given timeout elapses. After this method returnstrue, subsequentisTerminated()method calls will also returntrue.After this method returns
true(and does not throw an exception), it is true that: No more tasks will be executed by thisTaskExecutorServiceand no tasks are currently executing. That is, subsequentsubmitorexecutemethods will not execute the submitted tasks but complete them exceptionally with anOperationCanceledException.- Parameters:
cancelToken- theCancellationTokenwhich can be used to stop waiting for the termination of thisTaskExecutorService. That is, if this method detects, that cancellation was requested, it will throw anOperationCanceledException. This argument cannot benull.timeout- the maximum time to wait for thisTaskExecutorServiceto terminate in the given time unit. This argument must be greater than or equal to zero.unit- the time unit of thetimeoutargument. This argument cannot benull.- Returns:
trueif thisTaskExecutorServicehas terminated before the timeout elapsed,falseif the timeout elapsed first. In case thisTaskExecutorServiceterminated prior to this call this method always returnstrue.- Throws:
IllegalArgumentException- thrown if the specified timeout value is negativeNullPointerException- thrown if any of the arguments isnullOperationCanceledException- thrown if cancellation request was detected by this method before thisTaskExecutorServiceterminated This exception is not thrown if thisTaskExecutorServicewas terminated prior to this method call.- See Also:
-