MCPcopy Create free account
hub / github.com/antlr/codebuff / LegacyAsyncDispatcher

Class LegacyAsyncDispatcher

output/java_guava/1.4.18/Dispatcher.java:147–194  ·  view source on GitHub ↗

Implementation of a #legacyAsync() dispatcher.

Source from the content-addressed store, hash-verified

145 */
146
147 private static final class LegacyAsyncDispatcher extends Dispatcher {
148
149 // This dispatcher matches the original dispatch behavior of AsyncEventBus.
150 //
151 // We can't really make any guarantees about the overall dispatch order for this dispatcher in
152 // a multithreaded environment for a couple reasons:
153 //
154 // 1. Subscribers to events posted on different threads can be interleaved with each other
155 // freely. (A event on one thread, B event on another could yield any of
156 // [a1, a2, a3, b1, b2], [a1, b2, a2, a3, b2], [a1, b2, b3, a2, a3], etc.)
157 // 2. It's possible for subscribers to actually be dispatched to in a different order than they
158 // were added to the queue. It's easily possible for one thread to take the head of the
159 // queue, immediately followed by another thread taking the next element in the queue. That
160 // second thread can then dispatch to the subscriber it took before the first thread does.
161 //
162 // All this makes me really wonder if there's any value in queueing here at all. A dispatcher
163 // that simply loops through the subscribers and dispatches the event to each would actually
164 // probably provide a stronger order guarantee, though that order would obviously be different
165 // in some cases.
166
167 /**
168 * Global event queue.
169 */
170 private final ConcurrentLinkedQueue<EventWithSubscriber> queue = Queues.newConcurrentLinkedQueue();
171
172 @Override
173 void dispatch(Object event, Iterator<Subscriber> subscribers) {
174 checkNotNull(event);
175 while (subscribers.hasNext()) {
176 queue.add(new EventWithSubscriber(event, subscribers.next()));
177 }
178 EventWithSubscriber e;
179 while ((e = queue.poll()) != null) {
180 e.subscriber.dispatchEvent(e.event);
181 }
182 }
183
184 private static final class EventWithSubscriber {
185 private final Object event;
186
187 private final Subscriber subscriber;
188
189 private EventWithSubscriber(Object event, Subscriber subscriber) {
190 this.event = event;
191 this.subscriber = subscriber;
192 }
193 }
194 }
195
196 /**
197 * Implementation of {@link #immediate()}.

Callers

nothing calls this directly

Calls 1

Tested by

no test coverage detected