|
@@ -28,6 +28,7 @@ public class DrainDispatcher extends AsyncDispatcher {
|
|
|
// and similar grotesqueries
|
|
|
private volatile boolean drained = false;
|
|
|
private final BlockingQueue<Event> queue;
|
|
|
+ final Object mutex;
|
|
|
|
|
|
public DrainDispatcher() {
|
|
|
this(new LinkedBlockingQueue<Event>());
|
|
@@ -36,6 +37,7 @@ public class DrainDispatcher extends AsyncDispatcher {
|
|
|
private DrainDispatcher(BlockingQueue<Event> eventQueue) {
|
|
|
super(eventQueue);
|
|
|
this.queue = eventQueue;
|
|
|
+ this.mutex = this;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -53,8 +55,10 @@ public class DrainDispatcher extends AsyncDispatcher {
|
|
|
@Override
|
|
|
public void run() {
|
|
|
while (!Thread.currentThread().isInterrupted()) {
|
|
|
- // !drained if dispatch queued new events on this dispatcher
|
|
|
- drained = queue.isEmpty();
|
|
|
+ synchronized (mutex) {
|
|
|
+ // !drained if dispatch queued new events on this dispatcher
|
|
|
+ drained = queue.isEmpty();
|
|
|
+ }
|
|
|
Event event;
|
|
|
try {
|
|
|
event = queue.take();
|
|
@@ -75,8 +79,10 @@ public class DrainDispatcher extends AsyncDispatcher {
|
|
|
return new EventHandler() {
|
|
|
@Override
|
|
|
public void handle(Event event) {
|
|
|
- drained = false;
|
|
|
- actual.handle(event);
|
|
|
+ synchronized (mutex) {
|
|
|
+ actual.handle(event);
|
|
|
+ drained = false;
|
|
|
+ }
|
|
|
}
|
|
|
};
|
|
|
}
|