由浅入深了解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过程
随机推荐
- -webkit-box
父容器 display: flex; justify-content: center;/*主轴*/ align-items: center; /*交叉轴*/ display: -webkit-box; ...
- EasyUI 的DataGrid中DateTime的格式化问题
想必用过EasyUI的朋友们都应该会遇到这样的情况吧:(下图) 在EasyUI中DataGrid中如果要显示DateTime的时间时候,便会显示上图这样的格式,很明显,这里的格式不会是我们想要的,我们 ...
- vue package.json 解析
vue package.json 中可以看到这个包的来源位置 在repository对象中的url中可以看到 所以当你要封装自己包的时候就可以改变这个地址去加载这个包(把你封装的包发到某个gi ...
- linux命令——chmod/chown
改变文件所有权chown 例如 sudo chown username myfile 1 myfile文件的所有权变为username. chown -R username /files/work 1 ...
- linux 安装vscode
滚动安装vscode 需要先添加源,然后install 以centos为例: sudo rpm --import https://packages.microsoft.com/keys/microso ...
- ifconfig设置ip时出现提示 ifconfig: SIOCSIFFLAGS: Address not available
一.笔者使用ifconfig观察网卡情况如下: root@jello:/# ifconfig eth0 eth0 Link encap:Ethernet HWaddr 00:00:00:00:00:0 ...
- JAVA基础补漏--可变参数
使用场景:当参数列表数据类型已确定,但参数的个数不确定,就可以用可变参数 格式: 修饰符 返回值类型 方法名(数据类型...变量名){} 原理:可变参数底层为一数组,可根据变量个数的不同,创建不同长度 ...
- Why not inherit from List<T>?
问题: When planning out my programs, I often start with a chain of thought like so: A football team is ...
- 【bzoj2333 & luoguP3273】棘手的操作(线段树合并)
题目传送门:bzoj2333 luoguP3273 这操作还真“棘手”..听说这题是可并堆题?然而我不会可并堆.于是我就写了线段数合并,然后调了一晚上,数据结构毁一生!!!QAQ…… 其实这题也可以把 ...
- 爬虫之Xpath案例
案例:使用XPath的爬虫 现在我们用XPath来做一个简单的爬虫,我们尝试爬取某个贴吧里的所有帖子,并且将该这个帖子里每个楼层发布的图片下载到本地. # tieba_xpath.py #!/usr/ ...