public interface TaskExecutorService extends TaskExecutor
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.
CompletionStage, etc.AbstractTaskExecutorService| Modifier and Type | Method and Description |
|---|---|
ListenerRef |
addTerminateListener(Runnable listener)
Adds a listener which is to be notified after this
TaskExecutorService terminates. |
default void |
awaitTermination(CancellationToken cancelToken)
Waits until this
TaskExecutorService will not execute any more
tasks. |
boolean |
isShutdown()
Checks whether this
TaskExecutorService accepts newly submitted
tasks or not. |
boolean |
isTerminated()
Checks whether this
TaskExecutorService may execute tasks
submitted to it tasks or not. |
void |
shutdown()
Shuts down this
TaskExecutorService, so that it will not execute
tasks submitted to it after this method call returns. |
void |
shutdownAndCancel()
Shuts down this
TaskExecutorService and cancels already
submitted tasks, so that it will not execute tasks submitted to it after
this method call returns. |
boolean |
tryAwaitTermination(CancellationToken cancelToken,
long timeout,
TimeUnit unit)
Waits until this
TaskExecutorService will not execute any more
tasks or the given timeout elapses. |
execute, execute, executeFunction, executeStagedvoid shutdown()
TaskExecutorService, 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 this
TaskExecutorService.
This method call is idempotent. That is, calling it multiple times must have no further effect.
shutdownAndCancel()void shutdownAndCancel()
TaskExecutorService and 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 CancellationToken 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 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 this
TaskExecutorService.
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.
shutdown()boolean isShutdown()
TaskExecutorService accepts newly submitted
tasks or not. This method returns true, if and only, if either
the #shutdown() shutdown() or the
shutdownAndCancel() method has been called.
Therefore if, this method returns true, subsequent submit
and execute method invocations will not execute submitted tasks
and will only complete them exceptionally with an
OperationCanceledException.true if this TaskExecutorService accepts newly
submitted tasks, false if it has been shut downisTerminated()boolean isTerminated()
TaskExecutorService may execute tasks
submitted to it tasks or not. If this method returns true, no
more tasks will be executed by this TaskExecutorService and no
tasks are currently executing. That is, if this method returns
true subsequent submit or execute methods will
not execute the submitted tasks but complete them exceptionally with an
OperationCanceledException.
Also if this method returns true, subsequent
awaitTermination method calls will return immediately without
throwing an exception (will not even check for cancellation).
true if this TaskExecutorService accepts newly
submitted tasks, false if it has been shut downisTerminated()ListenerRef addTerminateListener(Runnable listener)
TaskExecutorService terminates. If the listener has been already
terminated, the listener is notified immediately in this
addTerminateListener method call. Also, the listener may only
be called at most once (per registration).
Whenever these registered listeners are notified, the
isTerminated() method already returns
true and calling the awaitTermination or the
tryAwaitTermination method 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 shutdown or the
shutdownAndCancel methods or in any other
thread, as this TaskExecutorService desires. Therefore, the
listeners must be written conservatively.
The listener can be removed if no longer required to be notified by
calling the unregister method of the
returned reference.
listener - the Runnable whose run method is to be
called after this TaskExecutorService terminates. This argument
cannot be null.null.NullPointerException - thrown if the specified listener is
nullawaitTermination(CancellationToken),
tryAwaitTermination(CancellationToken, long, TimeUnit)default void awaitTermination(CancellationToken cancelToken)
TaskExecutorService will not execute any more
tasks. After this method returns (without throwing an exception),
subsequent isTerminated() method calls will return true.
After this method returns without throwing an exception, it is true that:
No more tasks will be executed by this TaskExecutorService and no
tasks are currently executing. That is, subsequent submit or
execute methods will not execute the submitted tasks but complete
them exceptionally with an OperationCanceledException.
The default implementation simply calls tryAwaitTermination until it
returns true.
cancelToken - the CancellationToken which can be used to
stop waiting for the termination of this TaskExecutorService.
That is, if this method detects, that cancellation was requested, it
will throw an OperationCanceledException.
This argument cannot be null.NullPointerException - thrown if the specified
CancellationToken is nullOperationCanceledException - thrown if
cancellation request was detected by this method before this
TaskExecutorService terminated. This exception is not thrown if
this TaskExecutorService was terminated prior to this method
call.addTerminateListener(Runnable)boolean tryAwaitTermination(CancellationToken cancelToken, long timeout, TimeUnit unit)
TaskExecutorService will not execute any more
tasks or the given timeout elapses. After this method returns
true, subsequent isTerminated() method calls will also
return true.
After this method returns true (and does not throw an exception),
it is true that: No more tasks will be executed by this
TaskExecutorService and no tasks are currently executing. That
is, subsequent submit or execute methods will not execute
the submitted tasks but complete them exceptionally with an
OperationCanceledException.
cancelToken - the CancellationToken which can be used to
stop waiting for the termination of this TaskExecutorService.
That is, if this method detects, that cancellation was requested, it
will throw an OperationCanceledException.
This argument cannot be null.timeout - the maximum time to wait for this
TaskExecutorService to terminate in the given time unit. This
argument must be greater than or equal to zero.unit - the time unit of the timeout argument. This argument
cannot be null.true if this TaskExecutorService has terminated
before the timeout elapsed, false if the timeout elapsed first.
In case this TaskExecutorService terminated prior to this call
this method always returns true.IllegalArgumentException - thrown if the specified timeout value
is negativeNullPointerException - thrown if any of the arguments is
nullOperationCanceledException - thrown if
cancellation request was detected by this method before this
TaskExecutorService terminated
This exception is not thrown if this TaskExecutorService was
terminated prior to this method call.addTerminateListener(Runnable)