概述

一般使用EventBus的组件类,类似下面这种方式:

public class SampleComponent extends Fragment
{@Overridepublic 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){}@Overridepublic void onDestroy(){super.onDestroy();EventBus.getDefault().unregister(this);}
}

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

代码中以onEvent开头的方法的作用?

register(this)就是去当前类,遍历所有的方法,找到onEvent开头的然后进行存储。

onEvent后面可以写四种,也就是上面出现的四个方法,决定了当前的方法最终在什么线程运行,怎么运行

register后,调用:

EventBus.getDefault().post(param);

调用也可以叫发布,只要把这个param发布出去,EventBus会在它内部存储的方法中,进行扫描,找到参数匹配的,就使用反射进行调用。

其实EventBus就是在内部存储了一堆onEvent开头的方法,然后post的时候,根据post传入的参数,去找到匹配的方法,反射调用之。

它内部使用了Map进行存储,键就是参数的Class类型。知道是这个类型,根据post传入的参数就可以进行查找.


register

EventBus.getDefault().register(this);

首先:

EventBus.getDefault()是个单例模式,和传统的getInstance一样:

public static EventBus getDefault() {if(defaultInstance == null) {Class var0 = EventBus.class;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;}
}

核心代码说明:

代码中clazz.getMethods();去得到所有的方法:

就开始遍历每一个方法了,去匹配封装了。

分别判断了是否以onEvent开头,是否是public且非static和abstract方法,是否是一个参数。如果都复合,才进入封装的部分。

根据方法的后缀,来确定threadMode,threadMode是个枚举类型:就四种情况。

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

代码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

根据subscriberMethod.eventType,去subscriptionsByEventType去查找一个CopyOnWriteArrayList<Subscription> ,如果没有则创建。

然后传入的参数封装成了一个:Subscription(subscriber, subscriberMethod, priority);

其实subscriptionsByEventType是个Map,key:eventType ; value:CopyOnWriteArrayList<Subscription> ;

这个Map其实就是EventBus存储方法的地方,一定要记住!

代码中就是添加newSubscription;并且是按照优先级添加的。可以看到,优先级越高,会插到在当前List的前面。

代码中根据subscriber存储它所有的eventType ; 依然是map;key:subscriber ,value:List<eventType> ;知道就行,非核心代码,主要用于isRegister的判断。

代码中判断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;包含了执行改方法所需的一切。


post

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>() {@Overrideprotected PostingThreadState initialValue() {return new PostingThreadState();}
}

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

判断当前是否是UI线程。

遍历队列中的所有的event,调用postSingleEvent(eventQueue.remove(0), postingState)方法。

每次post都会去调用整个队列么,那么不会造成方法多次调用么?

可以看到代码中,有个判断,就是防止该问题的,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中。

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

遍历所有的Class,到subscriptionsByEventType去查找subscriptions;register里面把方法存的那个Map;

遍历每个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查找进行反射调用。

可以说,就是在一个单例内部维持着一个map对象存储了一堆的方法;post无非就是根据参数去查找方法,进行反射调用。


其余方法

介绍了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方法,无非都是一些状态判断,获取事件,移除事件的方法;

转载于:https://www.cnblogs.com/loaderman/p/6472229.html

EvenBus源码分析相关推荐

  1. Android—EventBus使用与源码分析

    EventBus 安卓事件发布/订阅框架 事件传递既可用于Android四大组件间通讯 EventBus的优点是代码简洁,使用简单,并将事件发布和订阅充分解耦 在onStart进行注册,onStop进 ...

  2. 【Golang源码分析】Go Web常用程序包gorilla/mux的使用与源码简析

    目录[阅读时间:约10分钟] 一.概述 二.对比: gorilla/mux与net/http DefaultServeMux 三.简单使用 四.源码简析 1.NewRouter函数 2.HandleF ...

  3. SpringBoot-web开发(四): SpringMVC的拓展、接管(源码分析)

    [SpringBoot-web系列]前文: SpringBoot-web开发(一): 静态资源的导入(源码分析) SpringBoot-web开发(二): 页面和图标定制(源码分析) SpringBo ...

  4. SpringBoot-web开发(二): 页面和图标定制(源码分析)

    [SpringBoot-web系列]前文: SpringBoot-web开发(一): 静态资源的导入(源码分析) 目录 一.首页 1. 源码分析 2. 访问首页测试 二.动态页面 1. 动态资源目录t ...

  5. SpringBoot-web开发(一): 静态资源的导入(源码分析)

    目录 方式一:通过WebJars 1. 什么是webjars? 2. webjars的使用 3. webjars结构 4. 解析源码 5. 测试访问 方式二:放入静态资源目录 1. 源码分析 2. 测 ...

  6. Yolov3Yolov4网络结构与源码分析

    Yolov3&Yolov4网络结构与源码分析 从2018年Yolov3年提出的两年后,在原作者声名放弃更新Yolo算法后,俄罗斯的Alexey大神扛起了Yolov4的大旗. 文章目录 论文汇总 ...

  7. ViewGroup的Touch事件分发(源码分析)

    Android中Touch事件的分发又分为View和ViewGroup的事件分发,View的touch事件分发相对比较简单,可参考 View的Touch事件分发(一.初步了解) View的Touch事 ...

  8. View的Touch事件分发(二.源码分析)

    Android中Touch事件的分发又分为View和ViewGroup的事件分发,先来看简单的View的touch事件分发. 主要分析View的dispatchTouchEvent()方法和onTou ...

  9. MyBatis原理分析之四:一次SQL查询的源码分析

    上回我们讲到Mybatis加载相关的配置文件进行初始化,这回我们讲一下一次SQL查询怎么进行的. 准备工作 Mybatis完成一次SQL查询需要使用的代码如下: Java代码   String res ...

最新文章

  1. Confluence 6 MySQL 输入你的数据库细节
  2. vivo系统如何没root激活Xposed框架的经验
  3. 【JFreeChart】JFreeChart—输出区域图
  4. aspen共沸精馏如何模拟_9月1011号Aspen plus:精馏精品培训!线上线下同时开展!另有惊喜活动等你参与!...
  5. Django后台管理之商品分类
  6. 2014北科计算机原理试题答案,北科_计算机组成原理考题-A卷答案
  7. 通过yum下载rpm包
  8. python制作个人信息管理系统_python实现简易学生信息管理系统
  9. erp系统是什么转型
  10. Black Hat USA 2020 大会主议题大盘点(上)
  11. 【VS Code配置matlab】手把手教学,matlab也能自动补全+瞬间启动+代码整理!
  12. 深度学习里面的正态分布
  13. 手把手原生js简单轮播图
  14. 一个基于对话框的简单MFC程序分析
  15. 基于BP神经网络的数字识别
  16. python时间格式转换为美式日期_python中有关时间日期格式转换问题
  17. Unity快速搭建城市场景
  18. Linux S3C2440 学习笔记02
  19. js骚操作骂人不带脏
  20. 农村淘宝年货节开20条品牌大街,1分钱买高品质牛奶!

热门文章

  1. 回车无法直接提交当前光标所在控件中的数据
  2. CentOS 7 更换 yum 源
  3. 如何用JavaScript实现获取验证码的效果
  4. ThreadLocal应用与原理分析
  5. 利用MYSQL的函数实现用户登录功能,进出都是JSON(第一版)
  6. AngularJS 无限滚动加载数据控件 ngInfiniteScroll
  7. 单碟1.75T 西数14TB充氦硬盘今年上
  8. 兼容性好的overflow CSS清除浮动一例
  9. 【Android】SlidingTabs
  10. time,gettimeofday,clock_gettime,_ftime