线程选择
/**
*
* @param subscription
* @param event
* @param isMainThread 发送通知时,所在线程
*/
private void postToSubscription(Subscription subscription, Object event, boolean isMainThread) {
//注解时,选择的处理线程
switch (subscription.subscriberMethod.threadMode) {
case POSTING:
invokeSubscriber(subscription, event);
break;
case MAIN://处理在主线程
if (isMainThread) { //发送在主线程,
invokeSubscriber(subscription, event);
} else {
mainThreadPoster.enqueue(subscription, event);
}
break;
case BACKGROUND:
if (isMainThread) {
backgroundPoster.enqueue(subscription, event);
} else {
invokeSubscriber(subscription, event);
}
break;
case ASYNC:
asyncPoster.enqueue(subscription, event);
break;
default:
throw new IllegalStateException("Unknown thread mode: " + subscription.subscriberMethod.threadMode);
}
}
其中呢,isMainThread是怎么来的呢
//根据looper判断postingState所在线程
postingState.isMainThread = Looper.getMainLooper() == Looper.myLooper();
postToSubscription(subscription, event, postingState.isMainThread);
//接下来,看看HandlerPoster是什么情况
//在EventBus创建时,就已经同时创建了
EventBus(EventBusBuilder builder) {
//....
mainThreadPoster = new HandlerPoster(this, Looper.getMainLooper(), 10);
backgroundPoster = new BackgroundPoster(this);
asyncPoster = new AsyncPoster(this);
//....
}
HandlerPoster
/**
* HandlerPoster就是一个Handler,只不过我们平时是在Activity里使用,这里抽取出来 * 跟我们平时没什么差别(主线程)
*/
final class HandlerPoster ext