Class DefaultExecutorListenerHelper<T>

  • Type Parameters:
    T - Interface for listeners to implement and called into with

    public class DefaultExecutorListenerHelper<T>
    extends ListenerHelper<T>
    This class ensures that listener execution will never happen on the thread that invokes ListenerHelper.call(). It does this in a different way from how the AsyncCallListenerHelper does it. In this implementation the iteration of the listeners still occurs on the thread executing the ListenerHelper.call(), but as listeners are added, it is ensured that they are provided an executor to execute on (so listener execution will actually happen on the executor). If a listener is provided with an executor, that provided Executor will NOT be overridden, and instead it will be used for the listeners execution.

    Internally this class uses the KeyDistributedExecutor, using the listener as the execution key, to ensure that any single listener will NEVER execute concurrently with itself.

    In general, this implementation is most efficient when there are few listeners, but the listeners are high complexity, or take a long time to execute. If you have few listeners AND they execute quickly, the normal ListenerHelper is likely a better choice. If you have MANY listeners, but they execute very quickly, AsyncCallListenerHelper is possibly a better choice.

    Unlike AsyncCallListenerHelper, even if the executor provided here is multi-threaded, order of listener call's are preserved. So there is no need to provide a single threaded executor into this class.

    Since:
    2.2.0
    • Constructor Detail

      • DefaultExecutorListenerHelper

        public DefaultExecutorListenerHelper​(java.lang.Class<? super T> listenerInterface,
                                             java.util.concurrent.Executor executor)
        Constructs a new DefaultExecutorListenerHelper that will handle listeners with the provided interface. The provided class MUST be an interface. If any listeners are not provided an executor, they will execute on the provided executor.
        Parameters:
        listenerInterface - Interface that listeners need to implement
        executor - Executor to execute listeners which were not provided one by default
    • Method Detail

      • addListener

        public void addListener​(T listener,
                                java.util.concurrent.Executor executor)
        Description copied from class: ListenerHelper
        Adds a listener to be executed on the next ListenerHelper.call() to this instance. If an executor is provided, on the next ListenerHelper.call() a task will be put on the executor to call this listener. If none is provided, the listener will be executed on the thread that is invoking the ListenerHelper.call().

        If an Executor is provided, and that Executor is NOT single threaded, the listener may be called concurrently. You can ensure this wont happen by using the KeyDistributedExecutor to get an executor from a single key, or by using the ExecutorLimiter with a limit of one, or an instance of the SingleThreadScheduler.

        Overrides:
        addListener in class ListenerHelper<T>
        Parameters:
        listener - Listener to be called when ListenerHelper.call() is invoked
        executor - Executor to call listener on, or null