Class AsyncCallListenerHelper<T>
- java.lang.Object
-
- org.threadly.concurrent.event.ListenerHelper<T>
-
- org.threadly.concurrent.event.AsyncCallListenerHelper<T>
-
- 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 classListenerHelper. In this implementation when listeners are invoked with theListenerHelper.call()function, the invocation of all the listeners will occur on theExecutorthat 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 theListenerHelper.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 thatListenerHelper.call()invocations will never block.To better clarify when this implementation makes sense compared to
ListenerHelperandDefaultExecutorListenerHelper. 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 normalListenerHelperis 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 normalListenerHelper, and just ensure that an executor is provided for every listener (but if you want to ensure a given listener is not executed concurrently theDefaultExecutorListenerHelperwill 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 theExecutorLimiterwith a limit of one, or an instance of theSingleThreadScheduler).- Since:
- 2.2.0
-
-
Constructor Summary
Constructors Constructor Description AsyncCallListenerHelper(java.lang.Class<? super T> listenerInterface, java.util.concurrent.Executor executor)Constructs a newAsyncCallListenerHelperthat will handle listeners with the provided interface.
-
Method Summary
-
Methods inherited from class org.threadly.concurrent.event.ListenerHelper
addListener, addListener, call, clearListeners, getSubscribedListeners, registeredListenerCount, removeListener
-
-
-
-
Constructor Detail
-
AsyncCallListenerHelper
public AsyncCallListenerHelper(java.lang.Class<? super T> listenerInterface, java.util.concurrent.Executor executor)
Constructs a newAsyncCallListenerHelperthat will handle listeners with the provided interface. The provided class MUST be an interface. AllListenerHelper.call()invocations will occur on the provided executor, but listeners may run on different executors if they are added with their respective executors.- Parameters:
listenerInterface- Interface that listeners need to implementexecutor-ExecutorthatListenerHelper.call()invocation will occur on
-
-