Class DefaultExecutorRunnableListenerHelper
- java.lang.Object
-
- org.threadly.concurrent.event.RunnableListenerHelper
-
- org.threadly.concurrent.event.DefaultExecutorRunnableListenerHelper
-
public class DefaultExecutorRunnableListenerHelper extends RunnableListenerHelper
This class ensures that listener execution will never happen on the thread that invokesRunnableListenerHelper.callListeners()
. It does this in a different way from how theAsyncCallRunnableListenerHelper
does it. In this implementation the iteration of the listeners still occurs on the thread executing theRunnableListenerHelper.callListeners()
, 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
RunnableListenerHelper
is likely a better choice. If you have MANY listeners, but they execute very quickly,AsyncCallRunnableListenerHelper
is possibly a better choice.Unlike
AsyncCallRunnableListenerHelper
, 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 DefaultExecutorRunnableListenerHelper(boolean callListenersOnce, java.util.concurrent.Executor executor)
Constructs a newDefaultExecutorRunnableListenerHelper
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addListener(java.lang.Runnable listener, java.util.concurrent.Executor executor)
Adds a listener to be called.-
Methods inherited from class org.threadly.concurrent.event.RunnableListenerHelper
addListener, addListener, callListeners, clearListeners, getSubscribedListeners, registeredListenerCount, removeListener
-
-
-
-
Constructor Detail
-
DefaultExecutorRunnableListenerHelper
public DefaultExecutorRunnableListenerHelper(boolean callListenersOnce, java.util.concurrent.Executor executor)
Constructs a newDefaultExecutorRunnableListenerHelper
. If any listeners are not provided an executor, they will execute on the provided executor.- Parameters:
callListenersOnce
-true
if listeners should only be called onceexecutor
-Executor
to execute listeners which were not provided one by default
-
-
Method Detail
-
addListener
public void addListener(java.lang.Runnable listener, java.util.concurrent.Executor executor)
Description copied from class:RunnableListenerHelper
Adds a listener to be called. If theRunnableListenerHelper
was constructed withtrue
(listeners can only be called once) then this listener will be called immediately. If the executor is null it will be called either on this thread or the thread callingRunnableListenerHelper.callListeners()
(depending on the previous condition).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 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 classRunnableListenerHelper
- Parameters:
listener
- runnable to call when trigger event calledexecutor
- executor listener should run on, ornull
-
-