private void postToSubscription(Subscription subscription, Object event, boolean isMainThread) {
switch (subscription.subscriberMethod.threadMode) {
case PostThread:
invokeSubscriber(subscription, event);
break;
case MainThread:
if (isMainThread) {
invokeSubscriber(subscription, event);
} else {
mainThreadPoster.enqueue(subscription, event);
}
break;
case BackgroundThread:
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);
}
}
根据threadMode判断,应该在哪个线程调用 反射方法
case PostThread:在当前线程,调用反射;
case MainThread:如果是主线程,则调用反射,否则用 handler发送消息,然后执行;
case BackgroundThread:如果是主线程,将任务加入到后台队列中,然后由EventBus中的一个线程池调用,如果是子线程,则直接调用反射;
case Async:与BackgroundThread一样,把任务加入到后台队列中,由EventBus中的一个线程池调用;且二者是同一个线程池;
BackgroundThread 与 Async区别:
BackgroundThread :它里边的任务一个接一个调用,中间用 boolean类型变量 handlerActive控制;
Async:动态控制并发;