EventBus源碼分析

簡介

前面我學習瞭如何使用EventBus,還有了解了EventBus的特性,那麼接下來咱們一塊兒來學習EventBus的源碼,查看EventBus的源碼,看看EventBus給咱們帶來什麼驚喜以及編程思想。java

EventBus-Android的發佈 - 訂閱

這個圖咱們從一開始就一直放置在上面了。咱們在來回顧一下,EventBus的官網是怎麼定義它的呢?
EventBus是Android和Java的發佈/訂閱(觀察者模式)事件總線。
咱們大概瞭解了EventBus的構建思想。接下來咱們進入源碼學習吧。git

進入源碼分析

咱們從EventBus的註冊開始入手。github

EventBus.getDefault().register(this);
public static EventBus getDefault() {
        if (defaultInstance == null) {
            synchronized (EventBus.class) {
                if (defaultInstance == null) {
                    //建立了EventBus實例,進入下面的方法
                    defaultInstance = new EventBus(); 
                }
            }
        }
        return defaultInstance;
    }

上面的方法是一個雙重校驗的單例。編程

public EventBus() {
        this(DEFAULT_BUILDER); //private static final EventBusBuilder DEFAULT_BUILDER = new EventBusBuilder();
    }

DEFAULT_BUILDEREventBusBuilder.class實例來建立的,這個類很是重要:使用自定義參數建立EventBus實例,還容許自定義默認EventBus實例,如前面的例子使用索引、配置EventBus的配置&事件的優先級&使用索引(四)等就是經過這個類來實現的,你們能夠回顧一下。進入初始化一下必要的參數的構造方法EventBus(EventBusBuilder builder),以下設計模式

EventBus(EventBusBuilder builder) {
   logger = builder.getLogger(); /初始化Logger
   subscriptionsByEventType = new HashMap<>(); //存儲訂閱事件的
   typesBySubscriber = new HashMap<>(); //存儲相關訂閱者class列表,key是註冊者的對象,value是訂閱者的class列表
   stickyEvents = new ConcurrentHashMap<>();  //粘性事件
   //下面這4個實例就是咱們設置方法``threadMode = ThreadMode.xxx``
   //*1
   mainThreadSupport = builder.getMainThreadSupport();
   mainThreadPoster = mainThreadSupport != null ? mainThreadSupport.createPoster(this) : null;
   backgroundPoster = new BackgroundPoster(this);
   asyncPoster = new AsyncPoster(this);
  //獲取註冊者信息索引(添加由EventBus的註釋預處理器生成的索引)
   indexCount = builder.subscriberInfoIndexes != null ? builder.subscriberInfoIndexes.size() : 0;
  //初始訂閱方法查找器。這個類主要具備查找訂閱的方法,繼續往下看
   subscriberMethodFinder = new SubscriberMethodFinder(builder.subscriberInfoIndexes,
   builder.strictMethodVerification, builder.ignoreGeneratedIndex);
   logSubscriberExceptions = builder.logSubscriberExceptions; //是否有日記訂閱,默認true
   logNoSubscriberMessages = builder.logNoSubscriberMessages;//是否有日記信息,默認 true
   sendSubscriberExceptionEvent = builder.sendSubscriberExceptionEvent;//是否發送訂閱異常事件,默認true
   sendNoSubscriberEvent = builder.sendNoSubscriberEvent;  //是否發送沒有訂閱事件,默認true
   throwSubscriberException = builder.throwSubscriberException; //是否拋出異常處理
  //默認狀況下,EventBus會考慮事件類層次結構(將通知超類的註冊者)。 關閉此功能將改善事件的發佈。對於直接擴展Object的簡單事件類,咱們測量事件發佈的速度提升了20%。
   eventInheritance = builder.eventInheritance; 
   executorService = builder.executorService;    //建立線程池
 }

咱們將此段代碼逐步分析.
這步主要是進行初始化話一下必要的參數,如代碼註解所示。
下面這段代碼就是咱們經常使用的@Subscribe(threadMode = ThreadMode.xxx);初始化。通常經常使用的就是如下4種。數組

mainThreadSupport = builder.getMainThreadSupport();
mainThreadPoster = mainThreadSupport != null ? mainThreadSupport.createPoster(this) : null;
backgroundPoster = new BackgroundPoster(this); 
asyncPoster = new AsyncPoster(this);

咱們來看看builder.getMainThreadSupport()方法返回的是MainThreadSupport接口,表示爲支持Android主線程。
上面的4個線程中都持有 PendingPostQueue 等待發送的隊列實例。
mainThreadSupport.createPoster(this)建立一個HandlerPoster而該類繼承了Handle,而且初始化了一個等待發布隊列。代碼以下。安全

public interface MainThreadSupport {
    boolean isMainThread();
    Poster createPoster(EventBus eventBus);
    class AndroidHandlerMainThreadSupport implements MainThreadSupport {
        private final Looper looper;
        public AndroidHandlerMainThreadSupport(Looper looper) {
            this.looper = looper;
        }
        @Override
        public boolean isMainThread() {
            return looper == Looper.myLooper();
        }
        @Override
        public Poster createPoster(EventBus eventBus) {
            return new HandlerPoster(eventBus, looper, 10);
        }
    }
}
subscriberMethodFinder = new SubscriberMethodFinder(builder.subscriberInfoIndexes,
                builder.strictMethodVerification, builder.ignoreGeneratedIndex);

上面代碼在這裏主要初始化訂閱的方法查找器。下面會講解到它是如何進行訂閱方法的。
咱們這這裏知道了EventBus初始化,而後相關的實例的建立,接下來我咱們進入到register(this)方法的調用以下方法。異步

public void register(Object subscriber) {
        //獲取當前註冊者對象的Class。
        Class<?> subscriberClass = subscriber.getClass(); 
        //經過該註冊者的`Class`來獲取當前註冊者裏面由`@Subscriber`註解綁定的方法。
        List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
        //必須在線程同步中進行。
        synchronized (this) {
            for (SubscriberMethod subscriberMethod : subscriberMethods) {
                subscribe(subscriber, subscriberMethod);
            }
        }
    }

上面的代碼表示的是給註冊者接收事件。 傳遞當前所註冊的對象,如Activity、Fragmentasync

  • 注意: 註冊者若是再也不接收信息,必須調用unregister(Object)方法,表示解除註冊則該訂閱者將再也不接收到數據了,若是不進行解除將可能出現內存泄漏。通常在onDestroy方法解除註冊後面會講解到。 在註冊者中擁有必須由@Subscribe註解的方法。@Subscribe還容許配置ThreadMode和優先級、是不是粘性行爲。

接着,咱們進入List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);方法。ide

//private static final Map<Class<?>, List<SubscriberMethod>> METHOD_CACHE = new ConcurrentHashMap<>(); //線程安全

List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {
  //從Map集合中獲取訂閱方法列表
   List<SubscriberMethod> subscriberMethods = METHOD_CACHE.get(subscriberClass);
  //判斷當前獲取方法是否爲空
  if (subscriberMethods != null) {
       return subscriberMethods;
  }
  //經過EventBusBuilder.ignoreGeneratedIndex
  //是否忽略配置索引,默認是忽略索引
  if (ignoreGeneratedIndex) {
    //1.經過反射獲取方法列表
     subscriberMethods = findUsingReflection(subscriberClass);
   } else {
    //2.經過索引方式獲取訂閱方法列表
     subscriberMethods = findUsingInfo(subscriberClass);
   }
  //是否訂閱方法爲空,若是爲空則表示該訂閱者裏面的方法都不符合EventBus訂閱方法的規則
  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;
   }
}

咱們逐步分析該段代碼。
該段代碼經過註冊者的類來獲取當前dingy的方法列表,若是不爲空則直接返回訂閱方法。
經過反射來查找訂閱方法,若是該方法爲空則拋出異常:該訂閱者的方法和其超類沒有@Subscriber註解的共有方法(即表示不符合EventBus訂閱方法的規則)。若是不爲空則存儲到ConcurrentHashMap集合中。
這裏的是由ConcurrentHashMap集合存儲是以當前訂閱者的classkey,而將其由@Subscriber綁定的方法添加到List中,,就是集合的value。必須保持線程安全的,因此這裏使用了ConcurrentHashMap

  • 1.是否忽略索引設置,該方法表示忽略設置索引,進入該方法findUsingReflection(Class<?> subscriberClass),經過反射進行獲取List<SubscriberMethod>
  • 注:設置索引在EventBus的配置&事件的優先級&使用索引(四)這裏說的,你們能夠去看看。
private List<SubscriberMethod> findUsingReflection(Class<?> subscriberClass) {
        FindState findState = prepareFindState();//準備查找狀態
        findState.initForSubscriber(subscriberClass);// 初始化訂閱者信息
        while (findState.clazz != null) {//循環獲取訂閱者的class是否爲空
            //經過反射獲取訂閱方法,
            // 3.該方法下面分析
            findUsingReflectionInSingleClass(findState); 
            findState.moveToSuperclass(); //刪除超類class
        }
           ////獲取訂閱方法list
        return getMethodsAndRelease(findState);
    }

上面的代碼主要是經過反射來獲取相關的訂閱方法,裏面由靜態內部類FindState進行管理相關信息,因爲上面的方法和下面索引的方法都將調用同一個方法,因此放在下面來將講解,請看下面信息。

  • 2.是否忽略索引設置,該方法表示的設置了索引,進入findUsingInfo(Class<?> subscriberClass)方法,以下。
private List<SubscriberMethod> findUsingInfo(Class<?> subscriberClass) {
        FindState findState = prepareFindState(); //準備查找狀態
        findState.initForSubscriber(subscriberClass);// 初始化訂閱者信息
        while (findState.clazz != null) { //循環獲取訂閱者的class是否爲空
            findState.subscriberInfo = getSubscriberInfo(findState);//獲取獲取訂閱者信息,這裏若是使用添加索引的話,這裏將能獲取到索引的相關信息。
            if (findState.subscriberInfo != null) { 
                //獲取訂閱方法
                SubscriberMethod[] array = findState.subscriberInfo.getSubscriberMethods();
                for (SubscriberMethod subscriberMethod : array) {
                 //進行等級檢查,並存儲到Map集合中,該方法checkAdd是FindState.class中的方法 ,key是@Subscriber中參數的class,value是該subscriberMethod.method,也就是@Subscribe中的方法如:public void onMessageEvent(MessageEvent event)
                 if (findState.checkAdd(subscriberMethod.method, subscriberMethod.eventType)) {
                        //將該訂閱方法添加到列表中
                        findState.subscriberMethods.add(subscriberMethod);
                    }
                }
            } else {
                //3.經過使用反射的方式來獲取,和上面忽略索引調用到同一個方法,這裏將解析
                findUsingReflectionInSingleClass(findState);
            }
            //清除父類的方法
            findState.moveToSuperclass();
        }
           //獲取訂閱方法list
        return getMethodsAndRelease(findState);
    }

分析本段代碼。
首先進入查找的準備狀態,經過默認狀態池返回狀態信息,主要存儲的是FindState實例。主要存儲的有訂閱者的class、訂閱者信息SubscriberInfo。訂閱者@Subscribe方法列表List<SubscriberMethod>等.
若是當前訂閱者class不爲空,則將獲取到訂閱者信息。這裏分兩種狀況。
若是使用添加索引的話,getSubscriberInfo(findState)該方法將獲取到訂閱信息,若是沒有使用索引的話則將調用findUsingReflectionInSingleClass(findState);該方法來獲取信息

  • 使用添加索引,並返回訂閱方法,該方法以下
//獲取訂閱方法
 SubscriberMethod[] array = findState.subscriberInfo.getSubscriberMethods();

因爲AbstractSubscriberInfo類實現了SubscriberInfo接口,而SimpleSubscriberInfo繼承了AbstractSubscriberInfo並實現了getSubscriberMethods方法,代碼以下:
本段代碼在SimpleSubscriberInfo.class中。返回訂閱方法數組SubscriberMethod[]

@Override
    public synchronized SubscriberMethod[] getSubscriberMethods() {
        int length = methodInfos.length;
        SubscriberMethod[] methods = new SubscriberMethod[length];
        for (int i = 0; i < length; i++) {
            SubscriberMethodInfo info = methodInfos[i];
            methods[i] = createSubscriberMethod(info.methodName, info.eventType, info.threadMode,
                    info.priority, info.sticky);
        }
        return methods;
    }
  • 3.若是沒有添加索引則進入該方法findUsingReflectionInSingleClass(findState);

經過反射來獲取訂閱者方法。這個方法有點長,慢慢看。

private void findUsingReflectionInSingleClass(FindState findState) {
        Method[] methods;
        try {
            // This is faster than getMethods, especially when subscribers are fat classes like Activities
            //該方法代替getMethods()方法,獲取該訂閱者class所有方法。
            methods = findState.clazz.getDeclaredMethods();
        } catch (Throwable th) {
            // Workaround for java.lang.NoClassDefFoundError, see https://github.com/greenrobot/EventBus/issues/149
            methods = findState.clazz.getMethods(); //獲取該訂閱者class所有方法
            findState.skipSuperClasses = true;  //設置跳過超類
        }
        for (Method method : methods) {
            int modifiers = method.getModifiers();//獲取修飾符
            if ((modifiers & Modifier.PUBLIC) != 0 && (modifiers & MODIFIERS_IGNORE) == 0) { //經過與運算判斷當前的修飾符,是否符合EventBus方法定義
                Class<?>[] parameterTypes = method.getParameterTypes(); //獲取參數類型
                if (parameterTypes.length == 1) { //由於EventBus方法中必須有參數,因此當參數爲1時,符合要求
                    //經過註解的形式@Subscribe獲取Subscribe,默認的Subscribe爲(priority==0,sticky=false,threadMode=POSTING),就是說ThreadMode爲POSTING,粘性爲false,優先級爲0,若是方法中設置了相應的值,則將是你設置的值。如threadMode=MAIN。
                    Subscribe subscribeAnnotation = method.getAnnotation(Subscribe.class);
                    if (subscribeAnnotation != null) {
                        Class<?> eventType = parameterTypes[0]; //獲取當前參數的class
                        //進行等級檢查,並存儲到Map集合中,該方法checkAdd是FindState.class中的方法 ,key是@Subscriber中參數的class,value是該subscriberMethod.method,也就是@Subscribe中的方法如:public void onMessageEvent(MessageEvent event)
                        if (findState.checkAdd(method, eventType)) { 
                            // 獲取ThreadMode ,如MAIN
                            ThreadMode threadMode = subscribeAnnotation.threadMode();
                            //將訂閱方法添加到列表中
                            findState.subscriberMethods.add(new SubscriberMethod(method, eventType, threadMode,
                                    subscribeAnnotation.priority(), subscribeAnnotation.sticky()));
                        }
                    }
                } else if (strictMethodVerification && method.isAnnotationPresent(Subscribe.class)) { //不符合EventBus訂閱方法的規則要求。拋出異常,提示該方法必須是@Subscribe註解,而且須要一個參數
                    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)) {//是一種非法的@Subscribe方法:必須是公共的,非靜態的,非抽象的
                String methodName = method.getDeclaringClass().getName() + "." + method.getName();
                throw new EventBusException(methodName +
                        " is a illegal @Subscribe method: must be public, non-static, and non-abstract");
            }
        }
    }

該段代碼主要是獲取該註冊者的所有方法,並進行篩選出來符合EventBus的訂閱方法的規則,經過註解的形式來獲取訂閱方法,最後添加到查找狀態的列表中。
首先獲取的是修飾符,參數類型,再經過註解的形式來獲取訂閱方法。以下註解Subscribe ,若是不符合EventBus相關訂閱方法的規則將拋出異常提示,是否在方法上書寫了錯誤的方法。
接下來咱們進入看看@Subscribe 訂閱方法是經過註解的形式來設置的。

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Subscribe {
     //ThreadMode ,默認POSTING
    ThreadMode threadMode() default ThreadMode.POSTING;
    //粘性事件,默認false
    boolean sticky() default false;
    //優先級,默認0
    int priority() default 0;
}

接下來進入getMethodsAndRelease(FindState findState)

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; //將查找狀態器findState,添加到狀態池FIND_STATE_POOL中,爲下次直接從查找狀態池中獲取
                    break;
                }
            }
        }
        return subscriberMethods; // 返回訂閱方法列表
    }

上面該方法經過FindState靜態內部類獲取了訂閱的列表,而後被存儲到了ConcurrentHashMap集合中。而且存儲到狀態池中,爲方便下次讀取。而且回收資源。
獲取到List<SubscriberMethod>訂閱列表後,再次回到了註冊方法中register(Object subscriber)進入到方法subscribe(subscriber, subscriberMethod);該方法經過循環進行遍歷

  • 注 此處必須是在線程同步中進行,因此添加synchronized。
//這裏的參數subscriber表示的是訂閱者,如Activity等,subscriberMethod表示訂閱方法,該方法就是在subscriber裏面
private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
        Class<?> eventType = subscriberMethod.eventType; //獲取事件類型,也就是參數的class
        Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
        //經過事件類型參數class獲取CopyOnWriteArrayList<Subscription>列表,該類你們能夠研究一下,
        //是寫時複製容器,即當咱們往`CopyOnWriteArrayList`容器添加元素時,先從原有的數組中拷貝一份出來,而後在新的容器作寫操做(添加元素),寫完以後,再將原來的數組引用指向到新數組的形式存儲數據的,它是線程安全的。
        CopyOnWriteArrayList<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
        if (subscriptions == null) {
            subscriptions = new CopyOnWriteArrayList<>();
            subscriptionsByEventType.put(eventType, subscriptions); //添加到Map集合中
        } else {
            if (subscriptions.contains(newSubscription)) { //是否已經存在了
                throw new EventBusException("Subscriber " + subscriber.getClass() + " already registered to event "
                        + eventType);
            }
        }
        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);//優先級高的添加添加到CopyOnWriteArrayList容器最前面,只有一個的時候就是添加在第一位了。
                break;
            }
        }
        //獲取訂閱事件的class列表,即訂閱方法的參數class列表
        List<Class<?>> subscribedEvents = typesBySubscriber.get(subscriber);
        if (subscribedEvents == null) {
            subscribedEvents = new ArrayList<>();
            //存儲到Map集合,key是訂閱者(即上面傳遞的Activity對象),value是訂閱事件列表
            typesBySubscriber.put(subscriber, subscribedEvents);
        }
        subscribedEvents.add(eventType); //將該事件類型class添加到訂閱事件列表中
        判斷當前方法是不是粘性的
        if (subscriberMethod.sticky) {
            if (eventInheritance) {
                //必須考慮eventType的全部子類的現有粘性事件
                // 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>).
                Set<Map.Entry<Class<?>, Object>> entries = stickyEvents.entrySet();
                for (Map.Entry<Class<?>, Object> entry : entries) {
                    Class<?> candidateEventType = entry.getKey();
                      isAssignableFrom(Class<?> cls)方法表示的類型是否能夠經過標識轉換或經過擴展引用轉換爲此類對象表示的類型,則返回true
                    if (eventType.isAssignableFrom(candidateEventType)) {
                        Object stickyEvent = entry.getValue();
                        //將粘性事件添加到訂閱者列表中
                        checkPostStickyEventToSubscription(newSubscription, stickyEvent);
                    }
                }
            } else {
                //將粘性事件添加到訂閱者列表中
                Object stickyEvent = stickyEvents.get(eventType);
                checkPostStickyEventToSubscription(newSubscription, stickyEvent);
            }
        }
    }

上面的方法中主要是經過獲取"事件類型"即訂閱方法中的參數class。而後將其添加到Map集合中,這裏用到了CopyOnWriteArrayList(原理:是寫時複製容器,即當咱們往CopyOnWriteArrayList容器添加元素時,先從原有的數組中拷貝一份出來,而後在新的容器作寫操做(添加元素),寫完以後,再將原來的數組引用指向到新數組的形式存儲數據的,它是線程安全的。)
而後進行判斷是否設置優先級,遍歷並添加到subscriptions列表中。
經過當前的註冊者,獲取註冊者class列表,並添加到typesBySubscriber集合中,key是註冊者的class,如:Activity,而value就是訂閱方法中的參數class的列表。這個地方說明了,一個註冊者,能夠擁有多個訂閱事件(方法),將其綁定起來,存儲到Map集合中。最後將該參數class添加到subscribedEvents.add(eventType);列表中。
下面方法是檢查粘性事件訂閱發送事件方法,以下

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.
            //這段代碼咱們到post發送事件的調用的時候講解。
            postToSubscription(newSubscription, stickyEvent, isMainThread());
        }
    }
  • 注:這裏主要是檢查是否發送粘性發送事件,方法 postToSubscription咱們到發送事件POST方法調用時講解。
  • 事件發送

接下來我進入post方法,事件發送。此時,咱們回憶一下簡介中的圖:
post()==>EventBus==>將分發到各個訂閱事件中。也就是訂閱方法。咱們來看看是如何進行操做的。代碼以下

EventBus.getDefault().post(new MessageEvent(message));

經過以上代碼我大概的知道,post()方法裏面的參數是一個實體的對象,而該實體就是咱們在訂閱方法中的參數實體。你發現了什麼了嗎?
前邊咱們已經分析了,該註冊者的訂閱方法主要的存儲過程,接下來咱們一塊兒進入探究吧。方法跟蹤進入post()方法。

/** Posts the given event to the event bus. */
    //給訂閱事件,發送事件總線
    public void post(Object event) {
        //獲取當前線程,即經過ThreadLocal本地線程來管理對應的事件線程,而且初始化發送線程狀態PostingThreadState
        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;
            }
        }
    }

currentPostingThreadState.get()方法中咱們知道

private final ThreadLocal<PostingThreadState> currentPostingThreadState = new ThreadLocal<PostingThreadState>() {
        @Override
        protected PostingThreadState initialValue() {
            return new PostingThreadState();
        }
    };

經過ThreadLocal這裏就叫"本地線程"吧,來管理當前的發送線程狀態,每一個發送線程將獲得對應一個PostingThreadState,而該PostingThreadState管理eventQueue信息,該信息主要存儲的是發送事件。
經過eventQueue.add(event)存儲該發送事件之後完成之後,就判斷當前的發送事件狀態是不是正在發送中,若是還沒發送則當該eventQueue不爲爲空的時候,進入循環發送該事件,發送完一個就刪除一個,直到發送完成爲止。
進入方法postSingleEvent(Object event, PostingThreadState postingState)中。

private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {
        Class<?> eventClass = event.getClass();
        boolean subscriptionFound = false;
        if (eventInheritance) {
            //查找全部事件類型
            List<Class<?>> eventTypes = lookupAllEventTypes(eventClass);
            int countTypes = eventTypes.size();
            for (int h = 0; h < countTypes; h++) {
                Class<?> clazz = eventTypes.get(h);
                //發送事件
                subscriptionFound |= postSingleEventForEventType(event, postingState, clazz);
            }
        } else {
            subscriptionFound = postSingleEventForEventType(event, postingState, eventClass);
        }
        if (!subscriptionFound) {
            if (logNoSubscriberMessages) {
                logger.log(Level.FINE, "No subscribers registered for event " + eventClass);
            }
            if (sendNoSubscriberEvent && eventClass != NoSubscriberEvent.class &&
                    eventClass != SubscriberExceptionEvent.class) {
                post(new NoSubscriberEvent(this, event));
            }
        }
    }

上面的方法首先獲取該事件類型的class而後傳遞到lookupAllEventTypes(eventClass)方法中,經過該方法獲取當前發送事件的class,包括父類、實現的接口等等列表。因此通常狀況下會返回當前發送的事件,和Object.class(注:固然了,若是該事件實現接口的話,也會包括實現的接口的。)列表即List<Class<?>>,而後進行遍歷進行或運算。
咱們先進入lookupAllEventTypes(eventClass)方法,看看返回的事件類型,而且知道他存儲到Map集合中,即key是當前事件class,value是該事件的全部類型,包括父類,接口等。

/** Looks up all Class objects including super classes and interfaces. Should also work for interfaces. */
    private static List<Class<?>> lookupAllEventTypes(Class<?> eventClass) {
        //private static final Map<Class<?>, List<Class<?>>> eventTypesCache = new HashMap<>(); key是當前事件類型的class,valu則是事件的全部類型,包括父類,接口等。本事例會獲得的是MessageEvent.class、和Object.class
        synchronized (eventTypesCache) {
            List<Class<?>> eventTypes = eventTypesCache.get(eventClass);
            if (eventTypes == null) {
                eventTypes = new ArrayList<>();
                Class<?> clazz = eventClass;
                while (clazz != null) {
                    eventTypes.add(clazz); //添加到列表中
                    //添加接口
                    addInterfaces(eventTypes, clazz.getInterfaces());
                    clazz = clazz.getSuperclass(); //獲取父類class
                }
                eventTypesCache.put(eventClass, eventTypes); //存儲d
            }
            return eventTypes;
        }
    }

看看方法postSingleEventForEventType(event, postingState, clazz),發送事件

private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class<?> eventClass) {
        CopyOnWriteArrayList<Subscription> subscriptions;
        synchronized (this) {
             //經過當前的事件class,獲取訂閱事件列表
            subscriptions = subscriptionsByEventType.get(eventClass);
        }
        if (subscriptions != null && !subscriptions.isEmpty()) {
            for (Subscription subscription : subscriptions) {
                //對PostingThreadState進行賦值
                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;
                }
            }
            return true;
        }
        return false;
    }

你們是否還記得在註冊的時候出現的CopyOnWriteArrayList<Subscription>在這裏就用到了,經過發送事件的class獲取CopyOnWriteArrayList容器裏面的訂閱事件信息,包括註冊者對象,訂閱方法等。而後遍歷並進行發送事件。在此方法發佈postToSubscription()事件

/**
  * subscription:發佈訂閱的事件處理器
  * event:當前發佈的事件
  * isMainThread:是不是在主線程中
  */
private void postToSubscription(Subscription subscription, Object event, boolean isMainThread) {
        switch (subscription.subscriberMethod.threadMode) {
            case POSTING:
                invokeSubscriber(subscription, event);
                break;
            case MAIN:
                if (isMainThread) {
                    invokeSubscriber(subscription, event);
                } else {
                    mainThreadPoster.enqueue(subscription, event);
                }
                break;
            case MAIN_ORDERED:
                if (mainThreadPoster != null) {
                    mainThreadPoster.enqueue(subscription, event);
                } else {
                    // temporary: technically not correct as poster not decoupled from subscriber
                    invokeSubscriber(subscription, event);
                }
                break;
            case BACKGROUND:
                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);
        }
    }

好了,這就是以前咱們在註冊中有一個地方遺留下的,就是判斷是不是粘性狀態是調用的方法,你們往回看,就看到了在方法checkPostStickyEventToSubscription中有調用到該方法。
這裏會根據,在方法中所選的threadMode進行調用,

  • 1.MAIN,若是當前是不是UI線程,則會進入到invokeSubscriber(subscription, event);方法中。不然將調用mainThreadPoster.enqueue(subscription, event);這個方法將交給HandlerPoster.class裏面的enqueue(Subscription subscription, Object event)方法,HandlerPoster.class現實了該接口,移交給handle完成,你們能夠進入HandlerPoster.class看看,這裏就不說了。
  • 2.POSTING直接調用 invokeSubscriber(subscription, event);
  • 3.MAIN_ORDERED若是當前mainThreadPoster不爲空則調用 mainThreadPoster.enqueue(subscription, event);MAIN進入HandlerPoster.class,反之調用invokeSubscriber(subscription, event);
  • 4.BACKGROUND若是當前是主線程,則調用backgroundPoster.enqueue(subscription, event);,反之調用invokeSubscriber(subscription, event);
  • 5.ASYNC,異步方法調用,執行 asyncPoster.enqueue(subscription, event);,方法進行跟蹤,該類有實現了Runnable接口,而後調用eventBus.invokeSubscriber(pendingPost);,最終將調用了invoke方法。
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);
        }
    }

invoke()這是一個本地的方法,將調用C/C++的到方法進行發佈。最後將進入到訂閱方法中。並傳遞該事件到到訂閱的方法中。
好了,這裏咱們將post發佈事件的方法簡單的分析了一下。整個EventBus的分析差很少完成了,可是,咱們還有一點不能忘記就是解除綁定。接下來咱們來看看解除綁定的方法。

  • 注:該方法通常生命週期的onDestroy()方法中進行解除綁定。

代碼以下:

EventBus.getDefault().unregister(this);

直接進入unregister(this)

/** Unregisters the given subscriber from all event classes. */
    public synchronized void unregister(Object subscriber) {
        List<Class<?>> subscribedTypes = typesBySubscriber.get(subscriber);
        if (subscribedTypes != null) {
            for (Class<?> eventType : subscribedTypes) {
                unsubscribeByEventType(subscriber, eventType);
            }
            typesBySubscriber.remove(subscriber);
        } else {
            logger.log(Level.WARNING, "Subscriber to unregister was not registered before: " + subscriber.getClass());
        }
    }
/** Only updates subscriptionsByEventType, not typesBySubscriber! Caller must update typesBySubscriber. */
    /**
     * subscriber:指代當前解除註冊的對象如Activity,
     * eventType:發送的事件類型,post函數的實體對象class
     */
private void unsubscribeByEventType(Object subscriber, Class<?> eventType) {
  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--;
            }
        }
   }
}

解除綁定,這裏很簡單。這裏就不一一進行解釋了,^_^。

總結

1.本篇文章主要對EventBus源碼進行簡單的分析。主要是進行了觀察者模式的一些高級運用。若是你們對觀察者模式理解不怎麼清楚能夠進入這裏看看簡單的案例觀察者模式,內容很是簡單。
2.相關的EvenBut的使用,請看以前的內容。如EventBus認識(一)EventBus的ThreadMode使用以及分析(二)等等。
3.學習本篇文章中能夠認識到一些經常使用的類如CopyOnWriteArrayListThreadLocal等,到時能夠深刻研究一下,有助咱們提升。
4.一些編程思想,設計模式值得咱們去學習的,如單例模式EventBus雙重校驗、建造者模式,EventBus構造器的時候用到,用了初始化各個參數等等。面向接口編程而不針對實現編程。
5.若是有什麼問題但願你們進行指正,好好學習,一塊兒進步。

相關文章
相關標籤/搜索