Class AsyncCallRunnableListenerHelper
- java.lang.Object
-
- org.threadly.concurrent.event.RunnableListenerHelper
-
- org.threadly.concurrent.event.AsyncCallRunnableListenerHelper
-
public class AsyncCallRunnableListenerHelper extends RunnableListenerHelper
This class changes the behavior of how listeners are called from the parent classRunnableListenerHelper
. In this implementation when listeners are invoked with thecallListeners()
function, the invocation of all the listeners will occur on theExecutor
that was provided at construction. If the listener was added without a provided executor it will then run on the provided executor (in the thread doing thecallListeners()
invocation, AKA it will run that listener before executing other listeners). If the listener was added with a provided executor, that listener will still execute on the provided executor (so not necessarily the executor provided at construction time).If it is desired that all listeners are executed asynchronously from each other, you should actually use the normal
RunnableListenerHelper
, and instead just ensure that an executor is provided when each listener is added. If you want listeners to execute concurrently from each other, but not concurrently for any single listener,DefaultExecutorRunnableListenerHelper
is likely a better choice. This class is only designed to ensure thatcallListeners()
invocations will never block.To better clarify when this implementation makes sense compared to
RunnableListenerHelper
andDefaultExecutorRunnableListenerHelper
. If you have a LOT of quick running listeners, this is the right class for you. If you have few listeners that execute quickly, then the normalRunnableListenerHelper
is likely a better choice. If you have long running/complex listeners,DefaultExecutorRunnableListenerHelper
is possibly the better choice. Alternative for the last condition you could use the normalRunnableListenerHelper
, and just ensure that an executor is provided for every listener (but if you want to ensure a given listener is not executed concurrently theDefaultExecutorRunnableListenerHelper
will handle this for you).- Since:
- 2.2.0
-
-
Constructor Summary
Constructors Constructor Description AsyncCallRunnableListenerHelper(boolean callListenersOnce, java.util.concurrent.Executor executor)
Constructs a newAsyncCallRunnableListenerHelper
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
callListeners()
Will call all listeners that are registered with this helper.-
Methods inherited from class org.threadly.concurrent.event.RunnableListenerHelper
addListener, addListener, addListener, clearListeners, getSubscribedListeners, registeredListenerCount, removeListener
-
-
-
-
Constructor Detail
-
AsyncCallRunnableListenerHelper
public AsyncCallRunnableListenerHelper(boolean callListenersOnce, java.util.concurrent.Executor executor)
Constructs a newAsyncCallRunnableListenerHelper
. This can call listeners one time, or every time callListeners is called.- Parameters:
callListenersOnce
-true
if listeners should only be called onceexecutor
-Executor
thatcallListeners()
should execute on
-
-
Method Detail
-
callListeners
public void callListeners()
Description copied from class:RunnableListenerHelper
Will call all listeners that are registered with this helper. If any listeners were provided without an executor, they will execute in the calling thread. No exceptions will be thrown in this calling thread if any exceptions occur from the listeners.If calling multiple times, this will only have an effect if constructed with a
false
, indicating that listeners can expect to be called multiple times. In which case all listeners that have registered will be called again. If this was constructed with the expectation of only calling once anIllegalStateException
will be thrown on subsequent calls.- Overrides:
callListeners
in classRunnableListenerHelper
-
-