簡(jiǎn)介
EventBus這東西相信很多人都用過(guò),是一種用于Android的事件發(fā)布-訂閱框架,由GreenRobot開(kāi)發(fā)谁榜,官方地址是:EventBus。它簡(jiǎn)化了應(yīng)用程序內(nèi)各個(gè)組件之間進(jìn)行通信的復(fù)雜度凡纳,尤其是Fragment之間進(jìn)行通信的問(wèn)題窃植,可以避免由于使用廣播通信而帶來(lái)的諸多不便。
此處我們并不探究EventBus的使用方法荐糜,而是通過(guò)深入理解常用接口的內(nèi)部邏輯來(lái)探究EventBus的整體內(nèi)部構(gòu)造巷怜,所以不熟悉EventBus的同學(xué)可以自行百度了解葛超。
通過(guò)unregister方法了解訂閱者(Subscriber)的緩存方式
首先我們先看unregister方法,一般反注冊(cè)方法都是直接清除訂閱者延塑,所以通過(guò)unregister方法來(lái)查找到訂閱者(Subscriber)的保存方式往往是最容易绣张。
public class EventBus {
......
private final Map<Class<?>, CopyOnWriteArrayList<Subscription>> subscriptionsByEventType;
private final Map<Object, List<Class<?>>> typesBySubscriber;
private final Map<Class<?>, Object> stickyEvents;
....
/** Unregisters the given subscriber from all event classes. */
public synchronized void unregister(Object subscriber) {
//typesBySubscriber用來(lái)保存(訂閱者和該訂閱者訂閱的事件類型)關(guān)系
//key:Subscriber,value:EventType列表
List<Class<?>> subscribedTypes = typesBySubscriber.get(subscriber);
if (subscribedTypes != null) {
//遍歷訂閱者對(duì)應(yīng)的事件類型列表,一個(gè)一個(gè)清除
for (Class<?> eventType : subscribedTypes) {
unsubscribeByEventType(subscriber, eventType);
}
//移除typesBySubscriber中訂閱者以及事件類型對(duì)應(yīng)關(guān)系
typesBySubscriber.remove(subscriber);
} else {
Log.w(TAG, "Subscriber to unregister was not registered before: " + subscriber.getClass());
}
}
/** Only updates subscriptionsByEventType, not typesBySubscriber! Caller must update typesBySubscriber. */
private void unsubscribeByEventType(Object subscriber, Class<?> eventType) {
//subscriptionsByEventType用來(lái)保存事件類型和Subscription列表對(duì)應(yīng)關(guān)系
//key:事件類型关带,value:Subscription里面包含訂閱者subscriber以及訂閱方法SubscriberMethod
List<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
if (subscriptions != null) {
int size = subscriptions.size();
//遍歷訂閱者并一一移除
for (int i = 0; i < size; i++) {
Subscription subscription = subscriptions.get(i);
if (subscription.subscriber == subscriber) {
subscription.active = false;
subscriptions.remove(i);
i--;
size--;
}
}
}
}
....
}
final class Subscription {
final Object subscriber;
final SubscriberMethod subscriberMethod;
/**
* Becomes false as soon as {@link EventBus#unregister(Object)} is called, which is checked by queued event delivery
* {@link EventBus#invokeSubscriber(PendingPost)} to prevent race conditions.
*/
volatile boolean active;
....
}
通過(guò)閱讀上面源碼侥涵,我們可以很容易的得到訂閱者和事件的緩存方式:
- subscriptionsByEventType保存了事件類型以及Subscription列表鍵值對(duì),事實(shí)上subscriptionsByEventType直接將事件類型、訂閱者Subscribery以及訂閱方法SubscriberMethod 聯(lián)系在一起宋雏;此處完全可以大膽推測(cè):發(fā)送事件是通過(guò)查找該事件類型對(duì)應(yīng)的Subscription列表芜飘,然后遍歷Subscription列表并一一觸發(fā)對(duì)應(yīng)的訂閱者Subscriber中SubscriberMethod 方法的調(diào)用,最終實(shí)現(xiàn)事件發(fā)送磨总。
-
typesBySubscriber保存了訂閱者以及訂閱者所訂閱的事件類型列表鍵值對(duì)嗦明;typesBySubscriber主要是在反注冊(cè)時(shí)用來(lái)輔助清理subscriptionsByEventType中的訂閱者。
register方法究竟做了什么
看完unregister方法蚪燕,我們可以大概的推測(cè)register(Object subscriber)里面主要做了什么:解析訂閱者subscriber里面的帶@Subscribe注解的方法娶牌,獲取監(jiān)聽(tīng)的事件類型,并將對(duì)應(yīng)關(guān)系保存到subscriptionsByEventType和typesBySubscriber中馆纳!
public class EventBus {
/**
* Registers the given subscriber to receive events. Subscribers must call {@link #unregister(Object)} once they
* are no longer interested in receiving events.
* <p/>
* Subscribers have event handling methods that must be annotated by {@link Subscribe}.
* The {@link Subscribe} annotation also allows configuration like {@link
* ThreadMode} and priority.
*/
public void register(Object subscriber) {
Class<?> subscriberClass = subscriber.getClass();
//查找訂閱者subscriber內(nèi)部的帶@Subscribe注解的回調(diào)方法
List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
synchronized (this) {
//遍歷監(jiān)聽(tīng)者帶@Subscribe注解的回調(diào)方法裙戏,進(jìn)而獲取并保存訂閱者和事件類型對(duì)應(yīng)關(guān)系
for (SubscriberMethod subscriberMethod : subscriberMethods) {
subscribe(subscriber, subscriberMethod);
}
}
}
// Must be called in synchronized block
private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
Class<?> eventType = subscriberMethod.eventType;
Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
//將subscriberMethod中的事件類型和訂閱者對(duì)應(yīng)關(guān)系保存到subscriptionsByEventType中
CopyOnWriteArrayList<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
if (subscriptions == null) {
subscriptions = new CopyOnWriteArrayList<>();
subscriptionsByEventType.put(eventType, subscriptions);
} else {
if (subscriptions.contains(newSubscription)) {
throw new EventBusException("Subscriber " + subscriber.getClass() + " already registered to event "
+ eventType);
}
}
//根據(jù)subscriberMethod中所帶的priority進(jìn)行排序,以便后續(xù)發(fā)送事件時(shí)快速的按優(yōu)先權(quán)排序發(fā)送
int size = subscriptions.size();
for (int i = 0; i <= size; i++) {
if (i == size || subscriberMethod.priority > subscriptions.get(i).subscriberMethod.priority) {
subscriptions.add(i, newSubscription);
break;
}
}
//將訂閱者subscriber和事件類型的對(duì)應(yīng)關(guān)系保存到typesBySubscriber中
List<Class<?>> subscribedEvents = typesBySubscriber.get(subscriber);
if (subscribedEvents == null) {
subscribedEvents = new ArrayList<>();
typesBySubscriber.put(subscriber, subscribedEvents);
}
subscribedEvents.add(eventType);
if (subscriberMethod.sticky) {//subscriberMethod被設(shè)置為接收粘性事件
if (eventInheritance) {//默認(rèn)為true厕诡,考慮父類繼承層級(jí)關(guān)系
// Existing sticky events of all subclasses of eventType have to be considered.
// Note: Iterating over all events may be inefficient with lots of sticky events,
// thus data structure should be changed to allow a more efficient lookup
// (e.g. an additional map storing sub classes of super classes: Class -> List<Class>).
//必須考慮eventType的所有子類的現(xiàn)有粘性事件累榜。
//注意:對(duì)大量粘性事件迭代所有事件可能效率低下,因此應(yīng)更改數(shù)據(jù)結(jié)構(gòu)以允許更有效的查找
//(例如灵嫌,存儲(chǔ)超類子類的附加映射:Class - > List <Class>)壹罚。
Set<Map.Entry<Class<?>, Object>> entries = stickyEvents.entrySet();
for (Map.Entry<Class<?>, Object> entry : entries) {
Class<?> candidateEventType = entry.getKey();
//判斷eventType是否為candidateEventType的父類
if (eventType.isAssignableFrom(candidateEventType)) {
Object stickyEvent = entry.getValue();
checkPostStickyEventToSubscription(newSubscription, stickyEvent);
}
}
} else {
Object stickyEvent = stickyEvents.get(eventType);
checkPostStickyEventToSubscription(newSubscription, stickyEvent);
}
}
}
private void checkPostStickyEventToSubscription(Subscription newSubscription, Object stickyEvent) {
if (stickyEvent != null) {
// If the subscriber is trying to abort the event, it will fail (event is not tracked in posting state)
// --> Strange corner case, which we don't take care of here.
postToSubscription(newSubscription, stickyEvent, Looper.getMainLooper() == Looper.myLooper());
}
}
}
上面代碼中,我們可以很明顯的看到register是如何保存訂閱者和事件類型對(duì)應(yīng)關(guān)系的寿羞,同時(shí)我們也知道了@Subscribe注解中priority 和sticky字段的作用猖凛。接下來(lái)我們繼續(xù)查看上面跳過(guò)的重要一步,探究SubscriberMethod是如何被找到的:
class SubscriberMethodFinder {
List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {
//從緩存中獲取subscriber內(nèi)部的訂閱回調(diào)方法
List<SubscriberMethod> subscriberMethods = METHOD_CACHE.get(subscriberClass);
if (subscriberMethods != null) {
return subscriberMethods;
}
if (ignoreGeneratedIndex) {//忽略索引绪穆,強(qiáng)制使用反射(默認(rèn)false)
subscriberMethods = findUsingReflection(subscriberClass);
} else {
subscriberMethods = findUsingInfo(subscriberClass);
}
if (subscriberMethods.isEmpty()) {
throw new EventBusException("Subscriber " + subscriberClass
+ " and its super classes have no public methods with the @Subscribe annotation");
} else {
//添加到緩存并返回
METHOD_CACHE.put(subscriberClass, subscriberMethods);
return subscriberMethods;
}
}
private List<SubscriberMethod> findUsingReflection(Class<?> subscriberClass) {
//生成findState 對(duì)象并初始化
FindState findState = prepareFindState();
findState.initForSubscriber(subscriberClass);
while (findState.clazz != null) {
//將subscriber中的SubscriberMethod信息封裝到findState中
findUsingReflectionInSingleClass(findState);
//切換到父類辨泳,進(jìn)入下個(gè)循環(huán)獲取父類的SubscriberMethod信息
findState.moveToSuperclass();
}
//回收數(shù)據(jù)并返回SubscriberMethod列表
return getMethodsAndRelease(findState);
}
private List<SubscriberMethod> findUsingInfo(Class<?> subscriberClass) {
//生成findState 對(duì)象并初始化
FindState findState = prepareFindState();
findState.initForSubscriber(subscriberClass);
while (findState.clazz != null) {
//從findState 和subscriberInfoIndexes中獲取subscriberInfo 列表,
//但目前發(fā)現(xiàn)源碼里面getSubscriberInfo(findState)返回的總是空的玖院,
//索引的使用跟開(kāi)發(fā)者的使用配置有關(guān)
findState.subscriberInfo = getSubscriberInfo(findState);
if (findState.subscriberInfo != null) {
SubscriberMethod[] array = findState.subscriberInfo.getSubscriberMethods();
for (SubscriberMethod subscriberMethod : array) {
if (findState.checkAdd(subscriberMethod.method, subscriberMethod.eventType)) {
findState.subscriberMethods.add(subscriberMethod);
}
}
} else {
/將subscriber中的SubscriberMethod信息通過(guò)反射封裝到findState中
findUsingReflectionInSingleClass(findState);
}
//切換到父類菠红,進(jìn)入下個(gè)循環(huán)獲取父類的SubscriberMethod信息
findState.moveToSuperclass();
}
//回收數(shù)據(jù)并返回SubscriberMethod列表
return getMethodsAndRelease(findState);
}
private SubscriberInfo getSubscriberInfo(FindState findState) {
//剛初始化的findState中findState.subscriberInfo總是空的
if (findState.subscriberInfo != null && findState.subscriberInfo.getSuperSubscriberInfo() != null) {
SubscriberInfo superclassInfo = findState.subscriberInfo.getSuperSubscriberInfo();
if (findState.clazz == superclassInfo.getSubscriberClass()) {
return superclassInfo;
}
}
//subscriberInfoIndexes是由EventBus中的靜態(tài)變量EventBusBuilder DEFAULT_BUILDER傳遞過(guò)來(lái)的,
//EventBusBuilder 中的addIndex(SubscriberInfoIndex index)沒(méi)有任何地方調(diào)用难菌,
//而且subscriberInfoIndexes也沒(méi)有使用add方法添加數(shù)據(jù)试溯,所以subscriberInfoIndexes一直都是空的
if (subscriberInfoIndexes != null) {
for (SubscriberInfoIndex index : subscriberInfoIndexes) {
SubscriberInfo info = index.getSubscriberInfo(findState.clazz);
if (info != null) {
return info;
}
}
}
//這里一直都返回空,所謂的索引其實(shí)一直沒(méi)用
return null;
}
private FindState prepareFindState() {
synchronized (FIND_STATE_POOL) {
for (int i = 0; i < POOL_SIZE; i++) {
FindState state = FIND_STATE_POOL[i];
if (state != null) {
FIND_STATE_POOL[i] = null;
return state;
}
}
}
return new FindState();
}
private void findUsingReflectionInSingleClass(FindState findState) {
Method[] methods;
//反射獲取訂閱者的所有方法
try {
// This is faster than getMethods, especially when subscribers are fat classes like Activities
methods = findState.clazz.getDeclaredMethods();
} catch (Throwable th) {
// Workaround for java.lang.NoClassDefFoundError, see https://github.com/greenrobot/EventBus/issues/149
methods = findState.clazz.getMethods();
findState.skipSuperClasses = true;
}
for (Method method : methods) {//遍歷所有方法
int modifiers = method.getModifiers();
//必須是public方法郊酒,不能是static和abstract方法
if ((modifiers & Modifier.PUBLIC) != 0 && (modifiers & MODIFIERS_IGNORE) == 0) {
Class<?>[] parameterTypes = method.getParameterTypes();
//必須只能有一個(gè)參數(shù)
if (parameterTypes.length == 1) {
Subscribe subscribeAnnotation = method.getAnnotation(Subscribe.class);
//必須添加@Subscribe注解
if (subscribeAnnotation != null) {
Class<?> eventType = parameterTypes[0];
if (findState.checkAdd(method, eventType)) {
//獲取注解中的信息并封裝到findState中
ThreadMode threadMode = subscribeAnnotation.threadMode();
findState.subscriberMethods.add(new SubscriberMethod(method, eventType, threadMode,
subscribeAnnotation.priority(), subscribeAnnotation.sticky()));
}
}
} else if (strictMethodVerification && method.isAnnotationPresent(Subscribe.class)) {
String methodName = method.getDeclaringClass().getName() + "." + method.getName();
throw new EventBusException("@Subscribe method " + methodName +
"must have exactly 1 parameter but has " + parameterTypes.length);
}
} else if (strictMethodVerification && method.isAnnotationPresent(Subscribe.class)) {
String methodName = method.getDeclaringClass().getName() + "." + method.getName();
throw new EventBusException(methodName +
" is a illegal @Subscribe method: must be public, non-static, and non-abstract");
}
}
}
private List<SubscriberMethod> getMethodsAndRelease(FindState findState) {
List<SubscriberMethod> subscriberMethods = new ArrayList<>(findState.subscriberMethods);
findState.recycle();
synchronized (FIND_STATE_POOL) {
for (int i = 0; i < POOL_SIZE; i++) {
if (FIND_STATE_POOL[i] == null) {
FIND_STATE_POOL[i] = findState;
break;
}
}
}
return subscriberMethods;
}
static class FindState {
final List<SubscriberMethod> subscriberMethods = new ArrayList<>();
final Map<Class, Object> anyMethodByEventType = new HashMap<>();
final Map<String, Class> subscriberClassByMethodKey = new HashMap<>();
final StringBuilder methodKeyBuilder = new StringBuilder(128);
Class<?> subscriberClass;
Class<?> clazz;
boolean skipSuperClasses;
SubscriberInfo subscriberInfo;
void initForSubscriber(Class<?> subscriberClass) {
this.subscriberClass = clazz = subscriberClass;
skipSuperClasses = false;
subscriberInfo = null;
}
void recycle() {
subscriberMethods.clear();
anyMethodByEventType.clear();
subscriberClassByMethodKey.clear();
methodKeyBuilder.setLength(0);
subscriberClass = null;
clazz = null;
skipSuperClasses = false;
subscriberInfo = null;
}
boolean checkAdd(Method method, Class<?> eventType) {
// 2 level check: 1st level with event type only (fast), 2nd level with complete signature when required.
// Usually a subscriber doesn't have methods listening to the same event type.
Object existing = anyMethodByEventType.put(eventType, method);
if (existing == null) {
return true;
} else {
if (existing instanceof Method) {
if (!checkAddWithMethodSignature((Method) existing, eventType)) {
// Paranoia check
throw new IllegalStateException();
}
// Put any non-Method object to "consume" the existing Method
anyMethodByEventType.put(eventType, this);
}
return checkAddWithMethodSignature(method, eventType);
}
}
private boolean checkAddWithMethodSignature(Method method, Class<?> eventType) {
methodKeyBuilder.setLength(0);
methodKeyBuilder.append(method.getName());
methodKeyBuilder.append('>').append(eventType.getName());
String methodKey = methodKeyBuilder.toString();
Class<?> methodClass = method.getDeclaringClass();
Class<?> methodClassOld = subscriberClassByMethodKey.put(methodKey, methodClass);
if (methodClassOld == null || methodClassOld.isAssignableFrom(methodClass)) {
// Only add if not already found in a sub class
return true;
} else {
// Revert the put, old class is further down the class hierarchy
subscriberClassByMethodKey.put(methodKey, methodClassOld);
return false;
}
}
void moveToSuperclass() {
if (skipSuperClasses) {
clazz = null;
} else {
clazz = clazz.getSuperclass();
String clazzName = clazz.getName();
/** Skip system classes, this just degrades performance. */
if (clazzName.startsWith("java.") || clazzName.startsWith("javax.") || clazzName.startsWith("android.")) {
clazz = null;
}
}
}
}
}
SubscriberMethod的查找是EventBus的核心業(yè)務(wù)邏輯之一遇绞,仔細(xì)閱讀源碼后發(fā)現(xiàn)其實(shí)并不是很復(fù)雜键袱,邏輯方法還是比較清晰的;唯一的問(wèn)題是作者引入了索引的概念摹闽,但源碼里面似乎并沒(méi)有完全實(shí)現(xiàn)蹄咖,索引邏輯沒(méi)有真正的觸發(fā)。索引流程必須得在開(kāi)發(fā)者完成相關(guān)配置后才能跑付鹿。
事件的發(fā)送
看完了注冊(cè)和反注冊(cè)邏輯澜汤,接下來(lái)就是事件的發(fā)送了。其實(shí)了解完注冊(cè)和反注冊(cè)倘屹,我們基本上已經(jīng)知道事件發(fā)送是怎么一回事了,無(wú)非就是拿著事件類型去subscriptionsByEventType中獲取該類型事件的訂閱者列表慢叨,然后遍歷列表觸發(fā)SubscriberMethod的調(diào)用纽匙,唯一值得期待的就只有注解中的線程模式的處理。廢話不多說(shuō)拍谐,繼續(xù)看源碼:
public class EventBus {
/** Posts the given event to the event bus. */
public void post(Object event) {
//獲取當(dāng)前發(fā)送狀態(tài)
PostingThreadState postingState = currentPostingThreadState.get();
//事件添加到發(fā)送隊(duì)列
List<Object> eventQueue = postingState.eventQueue;
eventQueue.add(event);
if (!postingState.isPosting) {//不在發(fā)送中
postingState.isMainThread = Looper.getMainLooper() == Looper.myLooper();
postingState.isPosting = true;//標(biāo)記正在發(fā)送
if (postingState.canceled) {
throw new EventBusException("Internal error. Abort state was not reset");
}
try {
while (!eventQueue.isEmpty()) {//循環(huán)發(fā)送事件
postSingleEvent(eventQueue.remove(0), postingState);
}
} finally {
postingState.isPosting = false;
postingState.isMainThread = false;
}
}
}
private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {
Class<?> eventClass = event.getClass();
boolean subscriptionFound = false;
if (eventInheritance) {//默認(rèn)為true烛缔,考慮父類繼承層級(jí)關(guān)系
//通過(guò)查看event繼承的父類層級(jí)關(guān)系來(lái)確認(rèn)有幾個(gè)事件類型
List<Class<?>> eventTypes = lookupAllEventTypes(eventClass);
int countTypes = eventTypes.size();
for (int h = 0; h < countTypes; h++) {
Class<?> clazz = eventTypes.get(h);
//根據(jù)事件類型發(fā)送事件
subscriptionFound |= postSingleEventForEventType(event, postingState, clazz);
}
} else {
//根據(jù)事件類型發(fā)送事件
subscriptionFound = postSingleEventForEventType(event, postingState, eventClass);
}
if (!subscriptionFound) {//沒(méi)找到訂閱者
if (logNoSubscriberMessages) {
Log.d(TAG, "No subscribers registered for event " + eventClass);
}
if (sendNoSubscriberEvent && eventClass != NoSubscriberEvent.class &&
eventClass != SubscriberExceptionEvent.class) {
post(new NoSubscriberEvent(this, event));
}
}
}
private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class<?> eventClass) {
//根據(jù)事件類型獲取Subscription列表
CopyOnWriteArrayList<Subscription> subscriptions;
synchronized (this) {
subscriptions = subscriptionsByEventType.get(eventClass);
}
if (subscriptions != null && !subscriptions.isEmpty()) {
//遍歷Subscription列表
for (Subscription subscription : subscriptions) {
postingState.event = event;
postingState.subscription = subscription;
boolean aborted = false;
try {
//事件發(fā)送給訂閱者
postToSubscription(subscription, event, postingState.isMainThread);
aborted = postingState.canceled;
} finally {
postingState.event = null;
postingState.subscription = null;
postingState.canceled = false;
}
if (aborted) {
break;
}
}
return true;
}
return false;
}
private void postToSubscription(Subscription subscription, Object event, boolean isMainThread) {
switch (subscription.subscriberMethod.threadMode) {
case POSTING://與事件發(fā)送源同一線程
//訂閱者的SubscriberMethod回調(diào)
invokeSubscriber(subscription, event);
break;
case MAIN://主線程
if (isMainThread) {
//訂閱者的SubscriberMethod回調(diào)
invokeSubscriber(subscription, event);
} else {
mainThreadPoster.enqueue(subscription, event);
}
break;
case BACKGROUND://后臺(tái)線程
if (isMainThread) {
backgroundPoster.enqueue(subscription, event);
} else {
//訂閱者的SubscriberMethod回調(diào)
invokeSubscriber(subscription, event);
}
break;
case ASYNC://異步線程
asyncPoster.enqueue(subscription, event);
break;
default:
throw new IllegalStateException("Unknown thread mode: " + subscription.subscriberMethod.threadMode);
}
}
void invokeSubscriber(Subscription subscription, Object event) {
try {
subscription.subscriberMethod.method.invoke(subscription.subscriber, event);
} catch (InvocationTargetException e) {
handleSubscriberException(subscription, event, e.getCause());
} catch (IllegalAccessException e) {
throw new IllegalStateException("Unexpected exception", e);
}
}
}
看完上面的代碼,我們總結(jié)一下事件的發(fā)送流程:
1轩拨、將新的事件添加的發(fā)送隊(duì)列中践瓷,然后判斷是否正在發(fā)送事件:如果是則不做其他處理,發(fā)送隊(duì)列中事件會(huì)自動(dòng)發(fā)送亡蓉;如果不是則觸發(fā)事件發(fā)送邏輯并標(biāo)記為正在發(fā)送晕翠。進(jìn)入發(fā)送狀態(tài)后,發(fā)送隊(duì)列中的事件會(huì)被一一發(fā)送直到隊(duì)列為空砍濒。隊(duì)列事件發(fā)送完了淋肾,標(biāo)記為未發(fā)送狀態(tài)。
2爸邢、從隊(duì)列中拿出待發(fā)送事件樊卓,通過(guò)查看事件event繼承的父類層級(jí)關(guān)系來(lái)確認(rèn)有幾個(gè)事件類型,然后再根據(jù)事件類型的個(gè)數(shù)來(lái)確認(rèn)事件event發(fā)幾次杠河。
3碌尔、根據(jù)各個(gè)事件類型獲取訂閱者列表,然后一一觸發(fā)訂閱者的SubscriberMethod方法券敌,進(jìn)而觸發(fā)回調(diào)唾戚。SubscriberMethod的觸發(fā)涉及到線程的處理,不過(guò)這里面的線程處理都是最基礎(chǔ)的處理方式待诅,沒(méi)有什么特別值得深究的颈走。mainThreadPoster是繼承Handler;asyncPoster和backgroundPoster都是繼承Runnable咱士,然后都被提交到線程池executorService中運(yùn)行立由。