Class AbstractTerminateNotifierTaskExecutorService

All Implemented Interfaces:
Executor, TaskExecutor, TaskExecutorService

public abstract class AbstractTerminateNotifierTaskExecutorService extends AbstractTaskExecutorService
An abstract base class for 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).

  • Constructor Details

    • AbstractTerminateNotifierTaskExecutorService

      public AbstractTerminateNotifierTaskExecutorService()
      Initializes the AbstractTerminateNotifierTaskExecutorService with no listeners currently registered.
  • Method Details

    • notifyTerminateListeners

      protected final void notifyTerminateListeners()
      Notifies the currently registered terminate listeners. This method may only be called if this 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.
    • addTerminateListener

      public ListenerRef addTerminateListener(Runnable listener)
      Adds a listener which is to be notified after this 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.

      Parameters:
      listener - the Runnable whose run method is to be called after this TaskExecutorService terminates. This argument cannot be null.
      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: