EventBus
EventBus copied to clipboard
Curios about 1000ms delay in BackgroundPoster
During systrace profiling I found this code in BackgroundPoster.java: https://github.com/greenrobot/EventBus/blob/3670d97835f15d0966e29fb8f52785ca3e87c2af/EventBus/src/org/greenrobot/eventbus/BackgroundPoster.java#L53-L61
I'm curios, do we really need to block background thread for 1 sec in case of empty queue? Thank you!
@greenrobot @greenrobot-team Hi, it'll be nice to have your comment here. Thank you!
`final class BackgroundPoster implements Runnable, Poster {
private final PendingPostQueue queue;
private final EventBus eventBus;
private volatile boolean executorRunning;
BackgroundPoster(EventBus eventBus) {
this.eventBus = eventBus;
queue = new PendingPostQueue();
}
public void enqueue(Subscription subscription, Object event) {
PendingPost pendingPost = PendingPost.obtainPendingPost(subscription, event);
synchronized (this) {
queue.enqueue(pendingPost);
if (!executorRunning) {
executorRunning = true;
eventBus.getExecutorService().execute(this);
}
}
}
@Override
public void run() {
try {
try {
while (true) {
PendingPost pendingPost = queue.poll(1000);
if (pendingPost == null) {
synchronized (this) {
// Check again, this time in synchronized
pendingPost = queue.poll();
if (pendingPost == null) {
executorRunning = false;
return;
}
}
}
eventBus.invokeSubscriber(pendingPost);
}
} catch (InterruptedException e) {
eventBus.getLogger().log(Level.WARNING, Thread.currentThread().getName() + " was interrupted", e);
}
} finally {
executorRunning = false;
}
}
} `
i thinks queue.poll(1000) can be delete
bacause firstly we enqueue(), then we execute(),after execute() ,it run().so when execute() is called,the PendingPostQueue's head variable will never be empty
@greenrobot @greenrobot-team Hi, it'll be nice to have your comment here. Thank you!