由浅入深了解EventBus:(六)
线程模型
在EventBus3.0框架中执行线程的快速切换,通过ThreadMode来指定线程在哪个线程中执行;
在EventBus3.0框架线程模型有个PendingPost 类负责数据的传递;
final class PendingPost {
private final static List<PendingPost> pendingPostPool = new ArrayList<PendingPost>();
Object event;
Subscription subscription;
PendingPost next;
private PendingPost(Object event, Subscription subscription) {
this.event = event;
this.subscription = subscription;
}
}
PendingPost 类中维护了3个字段,其中event为事件类的实例,subscription是监听回调信息封装,next 的类型也是一个PendingPost ,通过next可以构建一个列表;
在类的内部也维护着一个静态的PendingPost 对象的对象池,当需要PendingPost 实例时,首先从对象池中获取,当获取不到时在进行对象的new创建;
ThreadMode.MAIN 当调用线程不是主线程时,需要把事件执行推送到主线程中,在EventBus3.0框架中的EventBus类中维护着一个HandlerPoster对象来进行主线程数据的处理;HandlerPoster类是一个Handler,监听事件函数的执行应该在主线程回调的handleMessage 方法中,在源码中也确实是在handleMessage方法执行:
eventBus.invokeSubscriber(pendingPost); ThreadMode.Background 与ThreadMode.AsyncPoster 执行的方式差不多相同,都是从后台线程池中取出线程进行执行;在EventBus类中初始化了BackgroundPoster与AsyncPoster来对这2种线程模型进行处理,这2个类都继承了Runnable 接口;
final class BackgroundPoster implements Runnable {
private final PendingPostQueue queue;
private final EventBus eventBus;
private volatile boolean executorRunning;
BackgroundPoster(EventBus eventBus) {
this.eventBus = eventBus;
queue = new PendingPostQueue();
}
public void enqueue(Subscription subscription, Object event) {
PendingPost pendingPost = PendingPost.obtainPendingPost(subscription, event);
synchronized (this) {
queue.enqueue(pendingPost);
if (!executorRunning) {
executorRunning = true;
eventBus.getExecutorService().execute(this);
}
}
}
}
在BackgroundPoster 类中PendingPostQueue 是一个存储了PendingPost类型的队列,eventBus对应的就是EventBus类的实例,在BackgroundPoster 类中enqueue方法是在EventBus分发Post方法内部进行调用的;
eventBus.getExecutorService()获取EventBus类中的ExecutorService,在源码我们可以发现ExecutorService=Executors.newCachedThreadPool();
当执行eventBus.getExecutorService().execute(this);代码时,就跳转到BackgroundPoster 的run方法中
@Override
public void run() {
try {
try {
while (true) {
PendingPost pendingPost = queue.poll();
if (pendingPost == null) {
synchronized (this) {
// Check again, this time in synchronized
pendingPost = queue.poll();
if (pendingPost == null) {
executorRunning = false;
return;
}
}
}
eventBus.invokeSubscriber(pendingPost);
}
} catch (InterruptedException e) {
Log.w("Event", Thread.currentThread().getName() + " was interruppted", e);
}
} finally {
executorRunning = false;
}
}
从run方法中可以看出,最终执行的还是eventBus.invokeSubscriber(pendingPost) 方法;
由浅入深了解EventBus:(六)的更多相关文章
- 由浅入深了解EventBus:(五)
事件分发 EventBus3.0的事件的分发时通过EventBus类中的post(粘性事件为postSticky)方法,post与postSticky的唯一区别就是,在postSticky内部首先会向 ...
- 由浅入深了解EventBus:(四)
事件注册 在EventBus3.0框架中订阅者对事件进行注册/订阅是通过EventBus类中的register方法来实现的,register的方法参数就是我们的订阅者的实例; public void ...
- 由浅入深了解EventBus:(三)
原理 EventBus的核心工作机制如下图 在EventBus3.0架构图: EventBus类 在EventBus3.0框架的内部,核心类就是EventBus,订阅者的注册/订阅,解除注册,以及事件 ...
- 由浅入深了解EventBus:(二)
概念 深入学习EventBus框架,就必须理解EventBus的相关原理和一些概念: Subscribe 在EventBus框架中,消息的处理接收方法必须要“@Subscribe”注解来进行标注: p ...
- 由浅入深了解EventBus:(一)
概述 由greenrobot织贡献(该组织还贡献了greenDAO),一个Android事件发布/订阅轻量级框架; EventBus是一个消息总线,以观察者模式实现,用于简化程序的组件.线程通信,可以 ...
- C#总结(六)EventBus事件总线的使用-自己实现事件总线
在C#中,我们可以在一个类中定义自己的事件,而其他的类可以订阅该事件,当某些事情发生时,可以通知到该类.这对于桌面应用或者独立的windows服务来说是非常有用的.但对于一个web应用来说是有点问题的 ...
- 即时聊天APP(六) - 消息的接收以及EventBus使用
通常我们在接收消息的时候会有声音和震动的提示,因此我也加了代码达到这样的效果,这就要用到EventBus了,当然这里我也用到了自定义的广播,所以首先在Mainfests文件中加入以下代码: <r ...
- Elasticsearch由浅入深(六)批量操作:mget批量查询、bulk批量增删改、路由原理、增删改内部原理、document查询内部原理、bulk api的奇特json格式
mget批量查询 批量查询的好处就是一条一条的查询,比如说要查询100条数据,那么就要发送100次网络请求,这个开销还是很大的如果进行批量查询的话,查询100条数据,就只要发送1次网络请求,网络请求的 ...
- Spring源码由浅入深系列六 CreateBean过程
随机推荐
- maven tomcat7-maven-plugin配置及背景
背景: 在研发阶段,想让一个服务通过tomcat启动起来有很多的方法,常用的idea都有这样的支持,那么如果我们没有tomcat,能不能让服务通过tomcat启动起来呢?maven就提供了这样的支持. ...
- 5-es6的模块化开发与其它的不同
1.加载机制不同es是静态加载,其它是动态加载.Es6 模块的设计思想,是尽量的静态化,使得编译时就能确定模块的依赖关系,以及输入和输出的变量.CommonJS 和 AMD.CMD 模块,都只能在运行 ...
- yii2使用 db log
在本地测试的时候,输出log,还是输出到db中比较顺手. 配置过程: 1.加入log组件的配置: 'log' =>[ # 追踪级别 # 消息跟踪级别 # 在开发的时候,通常希望看到每个日志消息来 ...
- cocos2d: fullPathForFilename: No file found at /cc_2x2_white_image. Possible missing file.
程序运行的时候输出这条信息cocos2d: fullPathForFilename: No file found at /cc_2x2_white_image. Possible missing fi ...
- Java RSA公钥加密,私钥解密算法的尝试
https://www.cnblogs.com/liemng/p/6699257.html 写这篇博客其实是有点意外的,来源最初也算是入职当前这家公司算吧,由于项目要求数据几乎都进行了加密(政府项目么 ...
- Ubuntu16.04 Docker 安装
前提条件 Docker 要求 Ubuntu 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的 Ubuntu 版本是否支持 Docker. 通过 uname -r 命令查看你当前的内核版本 ...
- 使用selenium前学习HTML(3)——元素
<!-- HTML 元素指的是从开始标签(start tag)到结束标签(end tag)的所有代码. 注释:开始标签常被称为开放标签(opening tag),结束标签常称为闭合标签(clos ...
- oracle中add_months函数的用法
如果需要取上一个月的数据,并且每天都要进行此操作,每次都需要改时间,的确非常的麻烦,所以想到了oracle add_months函数这个函数 oracle add_months函数: oracle a ...
- Django学习笔记之django-debug-toolbar使用指南
介绍 django-debug-toolbar 是一组可配置的面板,可显示有关当前请求/响应的各种调试信息,并在单击时显示有关面板内容的更多详细信息. github地址 文档地址 安装 pip3 in ...
- CSS 图像拼合技术
CSS 图像拼合技术 一.图像拼合 图像拼合就是单个图像的集合. 有许多图像的网页可能需要很长的时间来加载和生成多个服务器的请求. 使用图像拼合会降低服务器的请求数量,并节省带宽. 二.图像拼合 - ...