- Type Parameters:
T- Interface for listeners to implement and called into with
public class AsyncCallListenerHelper<T> extends ListenerHelper<T>This class changes the behavior of how listeners are called from the parent class
ListenerHelper. In this implementation when listeners are invoked with the
ListenerHelper.call()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
ListenerHelper.call()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
ListenerHelper, 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,
DefaultExecutorListenerHelperis likely a better choice. This class is only designed to ensure that
ListenerHelper.call()invocations will never block.
To better clarify when this implementation makes sense compared to
DefaultExecutorListenerHelper. 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
ListenerHelperis likely a better choice. If you have long running/complex listeners,
DefaultExecutorListenerHelperis possibly the better choice. Alternative for the last condition you could use the normal
ListenerHelper, 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
DefaultExecutorListenerHelperwill handle this for you).
It is important to note that this class does not ensure ordering of how listeners are called. For example if you provided a multi-threaded executor, and are calling the listeners twice, those listeners call order is non-deterministic. If this is important to you, you must ensure that the Executor provided is single threaded (ie by using the
KeyDistributedExecutorto get an executor from a single key, or by using the
ExecutorLimiterwith a limit of one, or an instance of the
Methods inherited from class org.threadly.concurrent.event.ListenerHelper
addListener, addListener, call, clearListeners, getSubscribedListeners, registeredListenerCount, removeListener
public AsyncCallListenerHelper(java.lang.Class<? super T> listenerInterface, java.util.concurrent.Executor executor)Constructs a new
AsyncCallListenerHelperthat will handle listeners with the provided interface. The provided class MUST be an interface. All
ListenerHelper.call()invocations will occur on the provided executor, but listeners may run on different executors if they are added with their respective executors.
listenerInterface- Interface that listeners need to implement
ListenerHelper.call()invocation will occur on