public abstract class AbstractTerminateNotifierTaskExecutorService extends AbstractTaskExecutorService
TaskExecutorService implementations which
implements the addTerminateListener(Runnable) method as well as the
ones implemented by AbstractTaskExecutorService.
Rather than implementing the addTerminateListener method,
implementations need to call the #notifyTerminateListeners() method
after the TaskExecutorService terminated. This abstract class
provides a safe and robust handling of terminate listeners. The provided
implementation will guarantee that listeners will not be notified multiple
times and will be automatically unregistered after they have been notified
(allowing the listeners to be garbage collected, even if not unregistered
manually).
AbstractTaskExecutor.SubmittedTask<V>| Constructor and Description |
|---|
AbstractTerminateNotifierTaskExecutorService()
Initializes the
AbstractTerminateNotifierTaskExecutorService with
no listeners currently registered. |
| Modifier and Type | Method and Description |
|---|---|
ListenerRef |
addTerminateListener(Runnable listener)
Adds a listener which is to be notified after this
TaskExecutorService terminates. |
protected void |
notifyTerminateListeners()
Notifies the currently registered terminate listeners.
|
executeFunctionsubmitTaskclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitawaitTermination, isShutdown, isTerminated, shutdown, shutdownAndCancel, tryAwaitTerminationexecute, execute, executeStagedpublic AbstractTerminateNotifierTaskExecutorService()
AbstractTerminateNotifierTaskExecutorService with
no listeners currently registered.protected final void notifyTerminateListeners()
TaskExecutorService is already in the
terminated state (i.e.: isTerminated() returns
true. Note that once a TaskExecutorService is in the
terminated state it must remain in the terminated state forever after.
The AbstractTerminateNotifierTaskExecutorService actually relies
on this property for correctness.public 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.
Implementation note: Listeners added by this method will be automatically unregistered after they have been notified.
listener - the Runnable whose run method is to be
called after this TaskExecutorService terminates. This argument
cannot be null.null.TaskExecutorService.awaitTermination(CancellationToken),
TaskExecutorService.tryAwaitTermination(CancellationToken, long, TimeUnit)