post:就是把event,其实就是传入的 Javabean数据 , 保存在当前线程的 PostingThreadState 的 eventQueue队列 中
1>:首先:判断当前是否是主线程;
2>:然后:遍历 eventQueue队列中所有的 event,其实就是遍历所有 post(param) 中传递的参数,然后调用 postSingleEvent
private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {
// 获取当前 Javabean数据 所在的 Class对象
Class<? extends Object> eventClass = event.getClass();
// 根据eventClass获取 List<Class<?>>,其实就是 Javabean数据 的当前Class、
// 及父类和接口的Class类型,用于匹配
// eventTypes:是所有 Class
List<Class<?>> eventTypes = findEventTypes(eventClass);
boolean subscriptionFound = false;
// 遍历所有Class,在 subscriptionsByEventType中 查找 subscriptions ,
// register注册:就是把 所有方法都存储到 subscriptionsByEventType集合中
int countTypes = eventTypes.size();
for (int h = 0; h < countTypes; h++) {
Class<?> clazz = eventTypes.get(h);
CopyOnWriteArrayList<Subscription> subscriptions;
synchronized (this) {
subscriptions = subscriptionsByEventType.get(clazz);
}
if (subscriptions != null && !subscriptions.isEmpty()) {
// 遍历每个 subscription ,依次调用 postToSubscription
for (Subscription subscription : subscriptions) {
postingState.event = event;
postingState.subscription = subscription;
boolean aborted = false;
try {
postToSubscription(subscription, event, postingState.isMainThread);
aborted = postingState.canceled;
} finally {
postingState.event = null;
postingState.subscription = null;
postingState.canceled = false;
}
if (aborted) {
break;
}
}
subscriptionFound = true;
}
}
if (!subscriptionFound) {
if (eventClass != NoSubscriberEvent.class && eventClass != SubscriberExceptionEvent.class) {
post(new NoSubscriberEvent(this, event));
}
}
} |