Class AbstractTerminateNotifierTaskExecutorService
- All Implemented Interfaces:
Executor,TaskExecutor,TaskExecutorService
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).
-
Nested Class Summary
Nested classes/interfaces inherited from class org.jtrim2.executor.AbstractTaskExecutor
AbstractTaskExecutor.SubmittedTask<V> -
Constructor Summary
ConstructorsConstructorDescriptionInitializes theAbstractTerminateNotifierTaskExecutorServicewith no listeners currently registered. -
Method Summary
Modifier and TypeMethodDescriptionaddTerminateListener(Runnable listener) Adds a listener which is to be notified after thisTaskExecutorServiceterminates.protected final voidNotifies the currently registered terminate listeners.Methods inherited from class org.jtrim2.executor.AbstractTaskExecutorService
executeFunctionMethods inherited from class org.jtrim2.executor.AbstractTaskExecutor
submitTaskMethods 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, executeStagedMethods inherited from interface org.jtrim2.executor.TaskExecutorService
awaitTermination, isShutdown, isTerminated, shutdown, shutdownAndCancel, tryAwaitTermination
-
Constructor Details
-
AbstractTerminateNotifierTaskExecutorService
public AbstractTerminateNotifierTaskExecutorService()Initializes theAbstractTerminateNotifierTaskExecutorServicewith no listeners currently registered.
-
-
Method Details
-
notifyTerminateListeners
protected final void notifyTerminateListeners()Notifies the currently registered terminate listeners. This method may only be called if thisTaskExecutorServiceis already in the terminated state (i.e.:isTerminated()returnstrue. Note that once aTaskExecutorServiceis in the terminated state it must remain in the terminated state forever after. TheAbstractTerminateNotifierTaskExecutorServiceactually relies on this property for correctness. -
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.Implementation note: Listeners added by this method will be automatically unregistered after they have been notified.
- 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. - See Also:
-