研究了EventBus源码,不贴分析过程,有兴趣的同学自己查看阅读,这才是真程序员(PS:真不是我不擅长写文档,真的呦)。
源码地址:https://github.com/greenrobot/EventBus
官方文档地址:http://greenrobot.org/eventbus/
1. EventBus有几种ThreadMode:
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);
}
}
POSTING:默认模式,表示在当前线程执行
MAIN:在主线程中执行,如果post事件发生是主线程,则直接调用,如果不是,在