【EventBus】EventBus 源码解析 ( EventBus 构建 | EventBus 单例获取 | EventBus 构造函数 | EventBus 构建者 )
文章目錄
- 一、EventBus 單例獲取
- 二、EventBus 構(gòu)造函數(shù)
- 三、EventBus 構(gòu)建者
一、EventBus 單例獲取
在 EventBus 中 , 不論是進(jìn)行注冊(cè) , 取消注冊(cè) , 還是發(fā)送信息 , 都需要調(diào)用 EventBus.getDefault() , 獲取一個(gè) EventBus 的實(shí)例對(duì)象 , 這個(gè)實(shí)例對(duì)象是 單例對(duì)象 ;
在 EventBus.getDefault() 方法中 , 校驗(yàn)了 222 次 instance == null , 第一次校驗(yàn)先判空 , 然后加鎖后再次進(jìn)行第 222 次校驗(yàn) , 這是為了預(yù)防在第 111 次校驗(yàn)后 , 在創(chuàng)建 EventBus 前 , 突然在其它線程中創(chuàng)建了 EventBus 并且注冊(cè)了訂閱者 , 新創(chuàng)建的 EventBus 實(shí)例對(duì)象肯定會(huì)覆蓋舊的 EventBus , 但是舊的 EventBus 中的訂閱者就徹底消失了 ;
雙重加鎖校驗(yàn) 可以保證 EventBus 實(shí)例對(duì)象 全局 全時(shí)間線 唯一 ;
/*** EventBus是Java和Android的中央發(fā)布/訂閱事件系統(tǒng)。* 事件被發(fā)布({@link#post(Object)})到總線,總線將其傳遞給具有匹配處理程序的訂閱者* 事件類(lèi)型的方法。* 要接收事件,訂閱者必須使用{@link#register(Object)}將自己注冊(cè)到總線。* 一旦注冊(cè),訂閱服務(wù)器將接收事件,直到調(diào)用{@link#unregister(Object)}。* 事件處理方法必須由{@link Subscribe}注釋,必須是公共的,不返回任何內(nèi)容(void),* 并且只有一個(gè)參數(shù)(事件)。*/ public class EventBus {/** Convenience singleton for apps using a process-wide EventBus instance. */public static EventBus getDefault() {EventBus instance = defaultInstance;// 第一次校驗(yàn) if (instance == null) {synchronized (EventBus.class) {instance = EventBus.defaultInstance;// 第二次校驗(yàn) if (instance == null) {instance = EventBus.defaultInstance = new EventBus();}}}return instance;} }二、EventBus 構(gòu)造函數(shù)
在 EventBus.getDefault() 方法中 , 調(diào)用了 new EventBus() 方法創(chuàng)建了 EventBus 實(shí)例對(duì)象 , 在構(gòu)造函數(shù)中 , 又調(diào)用了 EventBus(EventBusBuilder builder) 構(gòu)造函數(shù) , 傳入默認(rèn)的 構(gòu)建者 EventBusBuilder 實(shí)例對(duì)象 ;
在 EventBus(EventBusBuilder builder) 構(gòu)造函數(shù)中 , 初始化了一堆數(shù)據(jù) ;
/*** EventBus是Java和Android的中央發(fā)布/訂閱事件系統(tǒng)。* 事件被發(fā)布({@link#post(Object)})到總線,總線將其傳遞給具有匹配處理程序的訂閱者* 事件類(lèi)型的方法。* 要接收事件,訂閱者必須使用{@link#register(Object)}將自己注冊(cè)到總線。* 一旦注冊(cè),訂閱服務(wù)器將接收事件,直到調(diào)用{@link#unregister(Object)}。* 事件處理方法必須由{@link Subscribe}注釋,必須是公共的,不返回任何內(nèi)容(void),* 并且只有一個(gè)參數(shù)(事件)。*/ public class EventBus {private static final EventBusBuilder DEFAULT_BUILDER = new EventBusBuilder();/*** 創(chuàng)建一個(gè)新的EventBus實(shí)例;每個(gè)實(shí)例都是一個(gè)單獨(dú)的作用域,在其中傳遞事件。要使用中央總線,請(qǐng)考慮{@link#getDefault()}。*/public EventBus() {this(DEFAULT_BUILDER);}EventBus(EventBusBuilder builder) {logger = builder.getLogger();subscriptionsByEventType = new HashMap<>();typesBySubscriber = new HashMap<>();stickyEvents = new ConcurrentHashMap<>();mainThreadSupport = builder.getMainThreadSupport();mainThreadPoster = mainThreadSupport != null ? mainThreadSupport.createPoster(this) : null;backgroundPoster = new BackgroundPoster(this);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;} }三、EventBus 構(gòu)建者
EventBusBuilder 構(gòu)建者中 , 有很多屬性值 , 其中可以進(jìn)行很多配置 ;
/*** 使用自定義參數(shù)創(chuàng)建EventBus實(shí)例,還允許安裝自定義默認(rèn)EventBus實(shí)例。使用{@link EventBus#builder()}創(chuàng)建一個(gè)新的生成器。*/ @SuppressWarnings("unused") public class EventBusBuilder {private final static ExecutorService DEFAULT_EXECUTOR_SERVICE = Executors.newCachedThreadPool();boolean logSubscriberExceptions = true;boolean logNoSubscriberMessages = true;boolean sendSubscriberExceptionEvent = true;boolean sendNoSubscriberEvent = true;boolean throwSubscriberException;boolean eventInheritance = true;boolean ignoreGeneratedIndex;boolean strictMethodVerification;ExecutorService executorService = DEFAULT_EXECUTOR_SERVICE;List<Class<?>> skipMethodVerificationForClasses;List<SubscriberInfoIndex> subscriberInfoIndexes;Logger logger;MainThreadSupport mainThreadSupport; }用戶可以自己配置 EventBusBuilder 構(gòu)建者 , 創(chuàng)建個(gè)性化的 EventBus , 并注冊(cè)訂閱者 ;
EventBus.builder().eventInheritance(false).sendNoSubscriberEvent(false).ignoreGeneratedIndex(false).logSubscriberExceptions(false).throwSubscriberException(false).build().register(this);總結(jié)
以上是生活随笔為你收集整理的【EventBus】EventBus 源码解析 ( EventBus 构建 | EventBus 单例获取 | EventBus 构造函数 | EventBus 构建者 )的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【EventBus】发布-订阅模式 (
- 下一篇: 【EventBus】EventBus 源