Skip to content

Instantly share code, notes, and snippets.

@vvsevolodovich
Created March 11, 2018 14:04
Show Gist options
  • Save vvsevolodovich/5499befbe8a9314347ffd606643dccea to your computer and use it in GitHub Desktop.
Save vvsevolodovich/5499befbe8a9314347ffd606643dccea to your computer and use it in GitHub Desktop.
Dispatcher
private static final class PerThreadQueuedDispatcher extends Dispatcher {
private final ThreadLocal<Queue<Event>> queue =
new ThreadLocal<Queue<Event>>() {
@Override
protected Queue<Event> initialValue() {
return Queues.newArrayDeque();
}
};
@Override
void dispatch(Object event, Iterator<Subscriber> subscribers) {
Queue<Event> queueForThread = queue.get();
queueForThread.offer(new Event(event, subscribers));
Event nextEvent;
while ((nextEvent = queueForThread.poll()) != null) {
while (nextEvent.subscribers.hasNext()) {
nextEvent.subscribers.next().dispatchEvent(nextEvent.event);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment