Created
March 11, 2018 14:04
-
-
Save vvsevolodovich/5499befbe8a9314347ffd606643dccea to your computer and use it in GitHub Desktop.
Dispatcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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