Android EventBus源代码解析 带你深入理解EventBus

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/40920453,本文出自:【张鸿洋的博客】

上一篇带大家初步了解了EventBus的使用方式,详见:Android EventBus实战 没听过你就out了,本篇博客将解析EventBus的源代码,相信能够让大家深入理解该框架的实现。也能解决非常多在使用中的疑问:为什么能够这么做?为什么这么做不好呢?

1、概述

一般使用EventBus的组件类,相似以下这样的方式:

public class SampleComponent extends Fragment
{

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		EventBus.getDefault().register(this);
	}

	public void onEventMainThread(param)
	{
	}
	
	public void onEventPostThread(param)
	{
		
	}
	
	public void onEventBackgroundThread(param)
	{
		
	}
	
	public void onEventAsync(param)
	{
		
	}
	
	@Override
	public void onDestroy()
	{
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}
	
}

大多情况下,都会在onCreate中进行register,在onDestory中进行unregister ;

看完代码大家也许会有一些疑问:

1、代码中另一些以onEvent开头的方法,这些方法是干嘛的呢?

在回答这个问题之前,我有一个问题,你咋不问register(this)是干嘛的呢?事实上register(this)就是去当前类,遍历全部的方法,找到onEvent开头的然后进行存储。

如今知道onEvent开头的方法是干嘛的了吧。

2、那onEvent后面的那些MainThread应该是什么标志吧?

嗯,是的,onEvent后面能够写四种,也就是上面出现的四个方法。决定了当前的方法终于在什么线程运行,怎么运行。能够參考上一篇博客或者细细往下看。


既然register了,那么肯定得说怎么调用是吧。

EventBus.getDefault().post(param);

调用非常easy,一句话,你也能够叫公布,仅仅要把这个param公布出去。EventBus会在它内部存储的方法中,进行扫描,找到參数匹配的,就使用反射进行调用。

如今有没有认为。撇开专业术语:事实上EventBus就是在内部存储了一堆onEvent开头的方法。然后post的时候,依据post传入的參数,去找到匹配的方法,反射调用之。

那么,我告诉你。它内部使用了Map进行存储。键就是參数的Class类型。知道是这个类型,那么你认为依据post传入的參数进行查找还是个事么?


以下我们就去看看EventBus的register和post真面目。

2、register

EventBus.getDefault().register(this);

首先:

EventBus.getDefault()事实上就是个单例,和我们传统的getInstance一个意思:

 /** Convenience singleton for apps using a process-wide EventBus instance. */
    public static EventBus getDefault() {
        if (defaultInstance == null) {
            synchronized (EventBus.class) {
                if (defaultInstance == null) {
                    defaultInstance = new EventBus();
                }
            }
        }
        return defaultInstance;
    }

使用了双重推断的方式。防止并发的问题,还能极大的提高效率。

然后register应该是一个普通的方法。我们去看看:

register公布给我们使用的有4个:

 public void register(Object subscriber) {
        register(subscriber, DEFAULT_METHOD_NAME, false, 0);
    }
 public void register(Object subscriber, int priority) {
        register(subscriber, DEFAULT_METHOD_NAME, false, priority);
    }
public void registerSticky(Object subscriber) {
        register(subscriber, DEFAULT_METHOD_NAME, true, 0);
    }
public void registerSticky(Object subscriber, int priority) {
        register(subscriber, DEFAULT_METHOD_NAME, true, priority);
    }

本质上就调用了同一个:

private synchronized void register(Object subscriber, String methodName, boolean sticky, int priority) {
        List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriber.getClass(),
                methodName);
        for (SubscriberMethod subscriberMethod : subscriberMethods) {
            subscribe(subscriber, subscriberMethod, sticky, priority);
        }
    }

四个參数

subscriber 是我们扫描类的对象,也就是我们代码中常见的this;

methodName 这个是写死的:“onEvent”,用于确定扫描什么开头的方法,可见我们的类中都是以这个开头。


sticky 这个參数,解释源代码的时候解释,临时不用管

priority 优先级,优先级越高,在调用的时候会越先调用。

以下開始看代码:

List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriber.getClass(),
                methodName);

调用内部类SubscriberMethodFinder的findSubscriberMethods方法,传入了subscriber 的class,以及methodName,返回一个List<SubscriberMethod>。

那么不用说,肯定是去遍历该类内部全部方法。然后依据methodName去匹配。匹配成功的封装成SubscriberMethod。最后返回一个List。以下看代码:

List<SubscriberMethod> findSubscriberMethods(Class<?

> subscriberClass, String eventMethodName) { String key = subscriberClass.getName() + '.' + eventMethodName; List<SubscriberMethod> subscriberMethods; synchronized (methodCache) { subscriberMethods = methodCache.get(key); } if (subscriberMethods != null) { return subscriberMethods; } subscriberMethods = new ArrayList<SubscriberMethod>(); Class<?> clazz = subscriberClass; HashSet<String> eventTypesFound = new HashSet<String>(); StringBuilder methodKeyBuilder = new StringBuilder(); while (clazz != null) { String name = clazz.getName(); if (name.startsWith("java.") || name.startsWith("javax.") || name.startsWith("android.")) { // Skip system classes, this just degrades performance break; } // Starting with EventBus 2.2 we enforced methods to be public (might change with annotations again) Method[] methods = clazz.getMethods(); for (Method method : methods) { String methodName = method.getName(); if (methodName.startsWith(eventMethodName)) { int modifiers = method.getModifiers(); if ((modifiers & Modifier.PUBLIC) != 0 && (modifiers & MODIFIERS_IGNORE) == 0) { Class<?>[] parameterTypes = method.getParameterTypes(); if (parameterTypes.length == 1) { String modifierString = methodName.substring(eventMethodName.length()); ThreadMode threadMode; if (modifierString.length() == 0) { threadMode = ThreadMode.PostThread; } else if (modifierString.equals("MainThread")) { threadMode = ThreadMode.MainThread; } else if (modifierString.equals("BackgroundThread")) { threadMode = ThreadMode.BackgroundThread; } else if (modifierString.equals("Async")) { threadMode = ThreadMode.Async; } else { if (skipMethodVerificationForClasses.containsKey(clazz)) { continue; } else { throw new EventBusException("Illegal onEvent method, check for typos: " + method); } } Class<?> eventType = parameterTypes[0]; methodKeyBuilder.setLength(0); methodKeyBuilder.append(methodName); methodKeyBuilder.append('>').append(eventType.getName()); String methodKey = methodKeyBuilder.toString(); if (eventTypesFound.add(methodKey)) { // Only add if not already found in a sub class subscriberMethods.add(new SubscriberMethod(method, threadMode, eventType)); } } } else if (!skipMethodVerificationForClasses.containsKey(clazz)) { Log.d(EventBus.TAG, "Skipping method (not public, static or abstract): " + clazz + "." + methodName); } } } clazz = clazz.getSuperclass(); } if (subscriberMethods.isEmpty()) { throw new EventBusException("Subscriber " + subscriberClass + " has no public methods called " + eventMethodName); } else { synchronized (methodCache) { methodCache.put(key, subscriberMethods); } return subscriberMethods; } }

呵,代码还真长;只是我们直接看核心部分:

22行:看到没。clazz.getMethods();去得到全部的方法:

23-62行:就開始遍历每个方法了,去匹配封装了。

25-29行:分别推断了是否以onEvent开头。是否是public且非static和abstract方法。是否是一个參数。假设都复合,才进入封装的部分。

32-45行:也比較简单,依据方法的后缀,来确定threadMode,threadMode是个枚举类型:就四种情况。

最后在54行:将method, threadMode, eventType传入构造了:new SubscriberMethod(method, threadMode, eventType)。加入到List,终于放回。

注意下63行:clazz = clazz.getSuperclass();能够看到。会扫描全部的父类,不仅仅是当前类。

继续回到register:

for (SubscriberMethod subscriberMethod : subscriberMethods) {
            subscribe(subscriber, subscriberMethod, sticky, priority);
        }

for循环扫描到的方法,然后去调用suscribe方法。

 // Must be called in synchronized block
    private void subscribe(Object subscriber, SubscriberMethod subscriberMethod, boolean sticky, int priority) {
        subscribed = true;
        Class<?> eventType = subscriberMethod.eventType;
        CopyOnWriteArrayList<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
        Subscription newSubscription = new Subscription(subscriber, subscriberMethod, priority);
        if (subscriptions == null) {
            subscriptions = new CopyOnWriteArrayList<Subscription>();
            subscriptionsByEventType.put(eventType, subscriptions);
        } else {
            for (Subscription subscription : subscriptions) {
                if (subscription.equals(newSubscription)) {
                    throw new EventBusException("Subscriber " + subscriber.getClass() + " already registered to event "
                            + eventType);
                }
            }
        }

        // Starting with EventBus 2.2 we enforced methods to be public (might change with annotations again)
        // subscriberMethod.method.setAccessible(true);

        int size = subscriptions.size();
        for (int i = 0; i <= size; i++) {
            if (i == size || newSubscription.priority > subscriptions.get(i).priority) {
                subscriptions.add(i, newSubscription);
                break;
            }
        }

        List<Class<?

>> subscribedEvents = typesBySubscriber.get(subscriber); if (subscribedEvents == null) { subscribedEvents = new ArrayList<Class<?>>(); typesBySubscriber.put(subscriber, subscribedEvents); } subscribedEvents.add(eventType); if (sticky) { Object stickyEvent; synchronized (stickyEvents) { stickyEvent = stickyEvents.get(eventType); } 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()); } } }

我们的subscriberMethod中保存了method, threadMode, eventType。上面已经说了。

4-17行:依据subscriberMethod.eventType,去subscriptionsByEventType去查找一个CopyOnWriteArrayList<Subscription> ,假设没有则创建。

顺便把我们的传入的參数封装成了一个:Subscription(subscriber, subscriberMethod, priority);

这里的subscriptionsByEventType是个Map,key:eventType ; value:CopyOnWriteArrayList<Subscription> ; 这个Map事实上就是EventBus存储方法的地方。一定要记住!

22-28行:实际上,就是加入newSubscription;而且是依照优先级加入的。

能够看到。优先级越高,会插到在当前List的前面。

30-35行:依据subscriber存储它全部的eventType 。 依旧是map;key:subscriber ,value:List<eventType> ;知道即可,非核心代码,主要用于isRegister的推断。

37-47行:推断sticky。假设为true,从stickyEvents中依据eventType去查找有没有stickyEvent。假设有则马上公布去运行。stickyEvent事实上就是我们post时的參数。

postToSubscription这种方法。我们在post的时候会介绍。


到此,我们register就介绍完了。

你仅仅要记得一件事:扫描了全部的方法。把匹配的方法终于保存在subscriptionsByEventType(Map,key:eventType ; value:CopyOnWriteArrayList<Subscription> )中。

eventType是我们方法參数的Class,Subscription中则保存着subscriber, subscriberMethod(method, threadMode, eventType), priority;包括了运行改方法所需的一切。


3、post

register完成,知道了EventBus怎样存储我们的方法了,以下看看post它又是怎样调用我们的方法的。

再看源代码之前,我们推測下:register时,把方法存在subscriptionsByEventType;那么post肯定会去subscriptionsByEventType去取方法,然后调用。

以下看源代码:

 /** Posts the given event to the event bus. */
    public void post(Object event) {
        PostingThreadState postingState = currentPostingThreadState.get();
        List<Object> eventQueue = postingState.eventQueue;
        eventQueue.add(event);

        if (postingState.isPosting) {
            return;
        } else {
            postingState.isMainThread = Looper.getMainLooper() == Looper.myLooper();
            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是一个ThreadLocal类型的,里面存储了PostingThreadState;PostingThreadState包括了一个eventQueue和一些标志位。

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

把我们传入的event,保存到了当前线程中的一个变量PostingThreadState的eventQueue中。

10行:推断当前是否是UI线程。

16-18行:遍历队列中的全部的event。调用postSingleEvent(eventQueue.remove(0), postingState)方法。

这里大家会不会有疑问。每次post都会去调用整个队列么,那么不会造成方法多次调用么?

能够看到第7-8行,有个推断,就是防止该问题的,isPosting=true了。就不会往下走了。


以下看postSingleEvent

private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {
        Class<? extends Object> eventClass = event.getClass();
        List<Class<?

>> eventTypes = findEventTypes(eventClass); boolean subscriptionFound = false; 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()) { 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) { Log.d(TAG, "No subscribers registered for event " + eventClass); if (eventClass != NoSubscriberEvent.class && eventClass != SubscriberExceptionEvent.class) { post(new NoSubscriberEvent(this, event)); } } }


将我们的event,即post传入的实參。以及postingState传入到postSingleEvent中。

2-3行:依据event的Class,去得到一个List<Class<?>>;事实上就是得到event当前对象的Class。以及父类和接口的Class类型;主要用于匹配。比方你传入Dog extends Dog,他会把Animal也装到该List中。

6-31行:遍历全部的Class,到subscriptionsByEventType去查找subscriptions;哈哈。熟不熟悉。还记得我们register里面把方法存哪了不?

是不是就是这个Map;

12-30行:遍历每个subscription,依次去调用postToSubscription(subscription, event, postingState.isMainThread);
这种方法就是去反射运行方法了。大家还记得在register。if(sticky)时,也会去运行这种方法。

以下看它怎样反射运行:

private void postToSubscription(Subscription subscription, Object event, boolean isMainThread) {
        switch (subscription.subscriberMethod.threadMode) {
        case PostThread:
            invokeSubscriber(subscription, event);
            break;
        case MainThread:
            if (isMainThread) {
                invokeSubscriber(subscription, event);
            } else {
                mainThreadPoster.enqueue(subscription, event);
            }
            break;
        case BackgroundThread:
            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);
        }
    }
前面已经说过subscription包括了全部运行须要的东西。大致有:subscriber, subscriberMethod(method, threadMode, eventType), priority;

那么这种方法:第一步依据threadMode去推断应该在哪个线程去运行该方法;
case PostThread:

  void invokeSubscriber(Subscription subscription, Object event) throws Error {
            subscription.subscriberMethod.method.invoke(subscription.subscriber, event);
  }

直接反射调用;也就是说在当前的线程直接调用该方法;

case MainThread:

首先去推断当前假设是UI线程,则直接调用;否则: mainThreadPoster.enqueue(subscription, event);把当前的方法加入到队列,然后直接通过handler去发送一个消息,在handler的handleMessage中,去运行我们的方法。

说白了就是通过Handler去发送消息,然后运行的。

 case BackgroundThread:

假设当前非UI线程。则直接调用。假设是UI线程。则将任务加入到后台的一个队列,终于由Eventbus中的一个线程池去调用

executorService = Executors.newCachedThreadPool();。

 case Async:将任务加入到后台的一个队列。终于由Eventbus中的一个线程池去调用;线程池与BackgroundThread用的是同一个。

这么说BackgroundThread和Async有什么差别呢?

BackgroundThread中的任务,一个接着一个去调用,中间使用了一个布尔型变量handlerActive进行的控制。

Async则会动态控制并发。


到此。我们完整的源代码分析就结束了。总结一下:register会把当前类中匹配的方法,存入一个map。而post会依据实參去map查找进行反射调用。分析这么久,一句话就说完了~~

事实上不用公布者。订阅者,事件。总线这几个词也许更好理解。以后大家问了EventBus,能够说,就是在一个单例内部维持着一个map对象存储了一堆的方法;post无非就是依据參数去查找方法,进行反射调用。


4、其余方法

介绍了register和post。大家获取还能想到一个词sticky,在register中。怎样sticky为true,会去stickyEvents去查找事件,然后马上去post;

那么这个stickyEvents何时进行保存事件呢?

事实上evevntbus中。除了post公布事件。另一个方法也能够:

 public void postSticky(Object event) {
        synchronized (stickyEvents) {
            stickyEvents.put(event.getClass(), event);
        }
        // Should be posted after it is putted, in case the subscriber wants to remove immediately
        post(event);
    }

和post功能相似,可是会把方法存储到stickyEvents中去;

大家再去看看EventBus中全部的public方法,无非都是一些状态推断,获取事件,移除事件的方法;没什么好介绍的。基本见名知意。


好了。到此我们的源代码解析就结束了,希望大家不仅能够了解这些优秀框架的内部机理,更能够体会到这些框架的非常多细节之处。并发的处理,非常多地方,为什么它这么做等等。




建了一个QQ群。方便大家交流。

群号:55032675

----------------------------------------------------------------------------------------------------------

博主部分视频已经上线。假设你不喜欢枯燥的文本。请猛戳(初录。期待您的支持):

1、高仿微信5.2.1主界面及消息提醒

2、高仿QQ5.0側滑

3、Android智能机器人“小慕”的实现
















原文地址:https://www.cnblogs.com/mengfanrong/p/5072775.html