image.png
官方地址: EventBus
EventBus.getDefault()
public class EventBus {
static volatile EventBus defaultInstance;
private static final EventBusBuilder DEFAULT_BUILDER = new EventBusBuilder();
private static final Map<Class<?>, List<Class<?>>> eventTypesCache = new HashMap<>();
private final Map<Class<?>, CopyOnWriteArrayList<Subscription>> subscriptionsByEventType;
private final Map<Object, List<Class<?>>> typesBySubscriber;
private final Map<Class<?>, Object> stickyEvents;
//1、單例設(shè)計模式返回EventBus對象
public static EventBus getDefault() {
EventBus instance = defaultInstance;
if (instance == null) {
synchronized (EventBus.class) {
instance = EventBus.defaultInstance;
if (instance == null) {
//2.調(diào)用EventBus構(gòu)造方法
instance = EventBus.defaultInstance = new EventBus();
}
}
}
return instance;
}
public EventBus() {
// 3思犁、調(diào)用有參構(gòu)造方法,傳入一個EventBusBuilder對象
this(DEFAULT_BUILDER);
}
EventBus(EventBusBuilder builder) {
logger = builder.getLogger();
//這個集合可以根據(jù)事件類型獲取訂閱者
//key:事件類型谋减,value:訂閱該事件的訂閱者集合
subscriptionsByEventType = new HashMap<>();
//訂閱者所訂閱的事件集合
//key:訂閱者,value:該訂閱者訂閱的事件集合
typesBySubscriber = new HashMap<>();
//粘性事件集合
//key:事件Class對象扫沼,value:事件對象
stickyEvents = new ConcurrentHashMap<>();
//Android主線程處理事件
mainThreadSupport = builder.getMainThreadSupport();
mainThreadPoster = mainThreadSupport != null ? mainThreadSupport.createPoster(this) : null;
//Background事件發(fā)送者
backgroundPoster = new BackgroundPoster(this);
//Async事件發(fā)送者
asyncPoster = new AsyncPoster(this);
//訂閱者訂閱事件查找對象
indexCount = builder.subscriberInfoIndexes != null ? builder.subscriberInfoIndexes.size() : 0;
subscriberMethodFinder = new SubscriberMethodFinder(builder.subscriberInfoIndexes,
builder.strictMethodVerification, builder.ignoreGeneratedIndex);
logSubscriberExceptions = builder.logSubscriberExceptions;
logNoSubscriberMessages = builder.logNoSubscriberMessages;
sendSubscriberExceptionEvent = builder.sendSubscriberExceptionEvent;
sendNoSubscriberEvent = builder.sendNoSubscriberEvent;
throwSubscriberException = builder.throwSubscriberException;
eventInheritance = builder.eventInheritance;
executorService = builder.executorService;
}
}
這個方法內(nèi)部首先通過單例模式創(chuàng)建一個EventBus對象出爹,在創(chuàng)建EventBus時最終會調(diào)用它的有參構(gòu)造函數(shù),傳入一個EventBus.Builder對象充甚。在這個有參構(gòu)造函數(shù)內(nèi)部對屬性進行初始化以政,其中有幾個比較重要的屬性:subscriptionsByEventType、typesBySubscriber伴找、stickyEvents和subscriberMethodFinder盈蛮,這幾個屬性的作用已經(jīng)在注釋中給出。
EventBus.getDefault().post(Object event)
public class EventBus {
private final ThreadLocal<PostingThreadState> currentPostingThreadState = new ThreadLocal<PostingThreadState>() {
@Override
protected PostingThreadState initialValue() {
return new PostingThreadState();
}
};
public void post(Object event) {
// 1技矮、獲取當(dāng)前線程的PostingThreadState抖誉,這是一個ThreadLocal對象
PostingThreadState postingState = currentPostingThreadState.get();
List<Object> eventQueue = postingState.eventQueue;
eventQueue.add(event);
if (!postingState.isPosting) {
postingState.isMainThread = isMainThread();
postingState.isPosting = true;
if (postingState.canceled) {
throw new EventBusException("Internal error. Abort state was not reset");
}
try {
while (!eventQueue.isEmpty()) {
postSingleEvent(eventQueue.remove(0), postingState);
}
} finally {
postingState.isPosting = false;
postingState.isMainThread = false;
}
}
}
}