@GwtIncompatible final class ListenerCallQueue<L> extends java.lang.Object
Listeners are registered once via addListener(L, java.util.concurrent.Executor)
and then may be invoked by enqueueing and then dispatching events.
The API of this class is designed to make it easy to achieve the following properties
#executor
.
directExecutor()
or be otherwise re-entrant (call back into your
object). So it is important to not call dispatch()
while holding any locks. This is why
enqueue(com.google.common.util.concurrent.ListenerCallQueue.Event<L>)
and dispatch()
are 2 different methods. It is expected that the decision
to run a particular event is made during the state change, but the decision to actually invoke
the listeners can be delayed slightly so that locks can be dropped. Also, because dispatch()
is expected to be called concurrently, it is idempotent.Modifier and Type | Class and Description |
---|---|
(package private) static interface |
ListenerCallQueue.Event<L>
Method reference-compatible listener event.
|
private static class |
ListenerCallQueue.PerListenerQueue<L>
A special purpose queue/executor that dispatches listener events serially on a configured
executor.
|
Modifier and Type | Field and Description |
---|---|
private java.util.List<ListenerCallQueue.PerListenerQueue<L>> |
listeners |
private static java.util.logging.Logger |
logger |
Constructor and Description |
---|
ListenerCallQueue() |
Modifier and Type | Method and Description |
---|---|
void |
addListener(L listener,
java.util.concurrent.Executor executor)
Adds a listener that will be called using the given executor when events are later
enqueued and dispatched . |
void |
dispatch()
Dispatches all events enqueued prior to this call, serially and in order, for every listener.
|
void |
enqueue(ListenerCallQueue.Event<L> event)
Enqueues an event to be run on currently known listeners.
|
void |
enqueue(ListenerCallQueue.Event<L> event,
java.lang.String label)
Enqueues an event to be run on currently known listeners, with a label.
|
private void |
enqueueHelper(ListenerCallQueue.Event<L> event,
java.lang.Object label) |
private static final java.util.logging.Logger logger
private final java.util.List<ListenerCallQueue.PerListenerQueue<L>> listeners
public void addListener(L listener, java.util.concurrent.Executor executor)
enqueued
and dispatched
.public void enqueue(ListenerCallQueue.Event<L> event)
The toString
method of the Event itself will be used to describe the event in the
case of an error.
event
- the callback to execute on dispatch()
public void enqueue(ListenerCallQueue.Event<L> event, java.lang.String label)
event
- the callback to execute on dispatch()
label
- a description of the event to use in the case of an errorprivate void enqueueHelper(ListenerCallQueue.Event<L> event, java.lang.Object label)
public void dispatch()
Note: this method is idempotent and safe to call from any thread