Class DefaultExecutorListenerHelper<T>
- java.lang.Object
-
- org.threadly.concurrent.event.ListenerHelper<T>
-
- org.threadly.concurrent.event.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 invokesListenerHelper.call()
. It does this in a different way from how theAsyncCallListenerHelper
does it. In this implementation the iteration of the listeners still occurs on the thread executing theListenerHelper.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 Summary
Constructors Constructor Description DefaultExecutorListenerHelper(java.lang.Class<? super T> listenerInterface, java.util.concurrent.Executor executor)
Constructs a newDefaultExecutorListenerHelper
that will handle listeners with the provided interface.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addListener(T listener, java.util.concurrent.Executor executor)
Adds a listener to be executed on the nextListenerHelper.call()
to this instance.-
Methods inherited from class org.threadly.concurrent.event.ListenerHelper
addListener, call, clearListeners, getSubscribedListeners, registeredListenerCount, removeListener
-
-
-
-
Constructor Detail
-
DefaultExecutorListenerHelper
public DefaultExecutorListenerHelper(java.lang.Class<? super T> listenerInterface, java.util.concurrent.Executor executor)
Constructs a newDefaultExecutorListenerHelper
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 implementexecutor
- 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 nextListenerHelper.call()
to this instance. If an executor is provided, on the nextListenerHelper.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 theListenerHelper.call()
.If an
Executor
is provided, and thatExecutor
is NOT single threaded, the listener may be called concurrently. You can ensure this wont happen by using theKeyDistributedExecutor
to get an executor from a single key, or by using theExecutorLimiter
with a limit of one, or an instance of theSingleThreadScheduler
.- Overrides:
addListener
in classListenerHelper<T>
- Parameters:
listener
- Listener to be called whenListenerHelper.call()
is invokedexecutor
-Executor
to call listener on, ornull
-
-