Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use Android Handler Async API to improve the performance #712

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,39 @@
*/
package org.greenrobot.eventbus;

import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;

public class HandlerPoster extends Handler implements Poster {
public class HandlerPoster implements Poster {

private Handler handler;
private final PendingPostQueue queue;
private final int maxMillisInsideHandleMessage;
private final EventBus eventBus;
private boolean handlerActive;

public HandlerPoster(EventBus eventBus, Looper looper, int maxMillisInsideHandleMessage) {
super(looper);
this.eventBus = eventBus;
this.maxMillisInsideHandleMessage = maxMillisInsideHandleMessage;
queue = new PendingPostQueue();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
handler = Handler.createAsync(looper, new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
return HandleMessageInHandlerPoster();
}
});
} else {
handler = new Handler(looper, new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
return HandleMessageInHandlerPoster();
}
});
}
}

public void enqueue(Subscription subscription, Object event) {
Expand All @@ -40,15 +56,14 @@ public void enqueue(Subscription subscription, Object event) {
queue.enqueue(pendingPost);
if (!handlerActive) {
handlerActive = true;
if (!sendMessage(obtainMessage())) {
if (!handler.sendMessage(handler.obtainMessage())) {
throw new EventBusException("Could not send handler message");
}
}
}
}

@Override
public void handleMessage(Message msg) {
private boolean HandleMessageInHandlerPoster() {
boolean rescheduled = false;
try {
long started = SystemClock.uptimeMillis();
Expand All @@ -60,22 +75,23 @@ public void handleMessage(Message msg) {
pendingPost = queue.poll();
if (pendingPost == null) {
handlerActive = false;
return;
return true;
}
}
}
eventBus.invokeSubscriber(pendingPost);
long timeInMethod = SystemClock.uptimeMillis() - started;
if (timeInMethod >= maxMillisInsideHandleMessage) {
if (!sendMessage(obtainMessage())) {
if (!handler.sendMessage(handler.obtainMessage())) {
throw new EventBusException("Could not send handler message");
}
rescheduled = true;
return;
return true;
}
}
} finally {
handlerActive = rescheduled;
}
}

}