public class AsyncCallRunnableListenerHelper extends RunnableListenerHelper
RunnableListenerHelper. In this implementation when listeners are invoked with the
callListeners()function, the invocation of all the listeners will occur on the
Executorthat 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 the
callListeners()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 that
callListeners() invocations will never block.
To better clarify when this implementation makes sense compared to
DefaultExecutorRunnableListenerHelper. 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 normal
RunnableListenerHelper is likely a better choice.
If you have long running/complex listeners,
possibly the better choice. Alternative for the last condition you could use the normal
RunnableListenerHelper, and just ensure that an executor is provided for every listener
(but if you want to ensure a given listener is not executed concurrently the
DefaultExecutorRunnableListenerHelper will handle this for you).
|Constructor and Description|
Constructs a new
|Modifier and Type||Method and Description|
Will call all listeners that are registered with this helper.
addListener, addListener, addListener, clearListeners, getSubscribedListeners, registeredListenerCount, removeListener
public AsyncCallRunnableListenerHelper(boolean callListenersOnce, java.util.concurrent.Executor executor)
AsyncCallRunnableListenerHelper. This can call listeners one time, or every time callListeners is called.
trueif listeners should only be called once
callListeners()should execute on
public void callListeners()
If calling multiple times, this will only have an effect if constructed with a
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 an
IllegalStateException will be thrown on