Class RunnableListenerHelper
- java.lang.Object
-
- org.threadly.concurrent.event.RunnableListenerHelper
-
- Direct Known Subclasses:
AsyncCallRunnableListenerHelper
,DefaultExecutorRunnableListenerHelper
public class RunnableListenerHelper extends java.lang.Object
Class which assist with holding and calling to Runnable listeners. In parallel designs it is common to have things subscribe for actions to occur (to later be alerted once an action occurs). This class makes it easy to allow things to register as a listener.For listener designs which are not using runnables, look at
ListenerHelper
.ListenerHelper
allows you to create similar designs while using any any interface to call back on.- Since:
- 2.2.0 (since 1.1.0 as org.threadly.concurrent.ListenerHelper)
-
-
Constructor Summary
Constructors Constructor Description RunnableListenerHelper(boolean callListenersOnce)
Constructs a newRunnableListenerHelper
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addListener(java.lang.Runnable listener)
Adds a listener to be called.void
addListener(java.lang.Runnable listener, java.util.concurrent.Executor executor)
Adds a listener to be called.void
addListener(java.lang.Runnable listener, java.util.concurrent.Executor queueExecutor, java.util.concurrent.Executor inThreadExecutionExecutor)
Adds a listener to be called.void
callListeners()
Will call all listeners that are registered with this helper.void
clearListeners()
Removes all listeners currently registered.java.util.Collection<java.lang.Runnable>
getSubscribedListeners()
Return a collection of the currently subscribed listener instances.int
registeredListenerCount()
Returns how many listeners were added, and will be ran on the next call tocallListeners
.boolean
removeListener(java.lang.Runnable listener)
Attempts to remove a listener waiting to be called.
-
-
-
Constructor Detail
-
RunnableListenerHelper
public RunnableListenerHelper(boolean callListenersOnce)
Constructs a newRunnableListenerHelper
. This can call listeners only once, or every timecallListeners()
is called.- Parameters:
callListenersOnce
-true
if listeners should only be called once
-
-
Method Detail
-
getSubscribedListeners
public java.util.Collection<java.lang.Runnable> getSubscribedListeners()
Return a collection of the currently subscribed listener instances. This returned collection can NOT be modified.- Returns:
- A non-null collection of currently subscribed listeners
-
callListeners
public void callListeners()
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.
-
addListener
public void addListener(java.lang.Runnable listener)
Adds a listener to be called. If theRunnableListenerHelper
was constructed withtrue
(listeners can only be called once) then this listener will be called immediately. This just defers to the other addListener call, providing null for the executor. So when the listener runs, it will be on the same thread as the one invokingcallListeners()
.- Parameters:
listener
- runnable to call when trigger event called
-
addListener
public void addListener(java.lang.Runnable listener, java.util.concurrent.Executor executor)
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 callingcallListeners()
(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
.- Parameters:
listener
- runnable to call when trigger event calledexecutor
- executor listener should run on, ornull
-
addListener
public void addListener(java.lang.Runnable listener, java.util.concurrent.Executor queueExecutor, java.util.concurrent.Executor inThreadExecutionExecutor)
Adds a listener to be called. If theRunnableListenerHelper
was constructed withtrue
(listeners can only be called once) then this listener will be called immediately.This allows you to provide different executors to use depending on the state of this
RunnableListenerHelper
. This is typically used as an optimization inside the threadly library only. Most users will be looking for theaddListener(Runnable, Executor)
which just provides that executor to both arguments here.- Parameters:
listener
- runnable to call when trigger event calledqueueExecutor
- executor listener should run on if this has to queue, ornull
inThreadExecutionExecutor
- executor listener should run on if this helpers state has transitioned to done
-
removeListener
public boolean removeListener(java.lang.Runnable listener)
Attempts to remove a listener waiting to be called.- Parameters:
listener
- listener instance to be removed- Returns:
true
if the listener was removed
-
clearListeners
public void clearListeners()
Removes all listeners currently registered.
-
registeredListenerCount
public int registeredListenerCount()
Returns how many listeners were added, and will be ran on the next call tocallListeners
. If this was constructed to only run once, all listeners will be removed after called, and thus this will report zero after callListeners has been invoked.- Returns:
- number of listeners registered to be called
-
-