文档章节

eventbus/documentation/delivery-threads-threadmode

s
 sarashare
发布于 2016/04/07 22:59
字数 489
阅读 29
收藏 0

Delivery Threads (ThreadMode)

EventBus can handle threading for you: events can be posted in threads different from the posting thread. A common use case is dealing with UI changes. In Android, UI changes must be done in the UI (main) thread. On the other hand, networking, or any time consuming task, must not run on the main thread. EventBus helps you to deal with those tasks and synchronize with the UI thread (without having to delve into thread transitions, using AsyncTask, etc).

In EventBus, you may define the thread that will call the event handling method by using one of the four ThreadModes.

ThreadMode: POSTING

Subscribers will be called in the same thread posting the event. This is the default. Event delivery is done synchronously and all subscribers will have been called once the posting is done. This ThreadMode implies the least overhead because it avoids thread switching completely. Thus this is the recommended mode for simple tasks that are known to complete is a very short time without requiring the main thread. Event handlers using this mode should return quickly to avoid blocking the posting thread, which may be the main thread. Example:
// Called in the same thread (default)

@Subscribe(threadMode = ThreadMode.POSTING) // ThreadMode is optional here
public void onMessage(MessageEvent event) {
    log(event.message);
}

ThreadMode: MAIN

Subscribers will be called in Android’s main thread (sometimes referred to as UI thread). If the posting thread is the main thread, event handler methods will be called directly (synchronously like described for ThreadMode.POSTING). Event handlers using this mode must return quickly to avoid blocking the main thread. Example:

// Called in Android UI's main thread
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessage(MessageEvent event) {
    textField.setText(event.message);
}

ThreadMode: BACKGROUND

Subscribers will be called in a background thread. If posting thread is not the main thread, event handler methods will be called directly in the posting thread. If the posting thread is the main thread, EventBus uses a single background thread that will deliver all its events sequentially. Event handlers using this mode should try to return quickly to avoid blocking the background thread.

// Called in the background thread
@Subscribe(threadMode = ThreadMode.BACKGROUND)
public void onMessage(MessageEvent event){
    saveToDisk(event.message);
}

ThreadMode: ASYNC

Event handler methods are called in a separate thread. This is always independent from the posting thread and the main thread. Posting events never wait for event handler methods using this mode. Event handler methods should use this mode if their execution might take some time, e.g. for network access. Avoid triggering a large number of long running asynchronous handler methods at the same time to limit the number of concurrent threads. EventBus uses a thread pool to efficiently reuse threads from completed asynchronous event handler notifications.

// Called in a separate thread
@Subscribe(threadMode = ThreadMode.ASYNC)
public void onMessage(MessageEvent event){
    backend.send(event.message);
}

© 著作权归作者所有

共有 人打赏支持
s
粉丝 0
博文 18
码字总数 6514
作品 0
崇明
私信 提问
自己动手写事件总线(EventBus)

本文由云+社区发表 事件总线核心逻辑的实现。 EventBus的作用 Android中存在各种通信场景,如之间的跳转,与以及其他组件之间的交互,以及在某个耗时操作(如请求网络)之后的callback回调等...

腾讯云加社区
2018/12/29
0
0
滴-EventBus-新司机考驾照

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27489007/article/details/84989802 EventBus缘起何方 她是由greenrobot组织贡献的一个基于观察者模式的A...

秋隆
2018/12/13
0
0
Android EventBus

本文节选自电子书《Netkiller Android 手札》 Netkiller Android 手札 http://www.netkiller.cn/android/index.html Mr. Neo Chan, 陈景峯(BG7NYT) 中国广东省深圳市望海路半岛城邦三期 5180...

netkiller-
2018/11/08
0
0
Android开源框架源码鉴赏:EventBus

作者:郭孝星 校对:郭孝星 关于项目 Android Open Framework analysis项目主要用来分析Android平台主流开源框架的源码与原理实现。 文章目录 一 注册订阅者 二 发布事件Event 三 接收事件E...

郭孝星
2018/03/27
0
0
Android框架之路——EventBus的使用

一、简介 EventBus是由greenrobot 组织贡献的一个Android事件发布/订阅轻量级框架。EventBus是一个Android端优化的publish/subscribe消息总线,简化了应用程序内各组件间、组件与后台线程间的...

天王盖地虎626
02/15
0
0

没有更多内容

加载失败,请刷新页面

加载更多

拒绝拖拽 使用ConstraintLayout优化你的布局吧

ConstraintLayout出现有一段时间了,不过一直没有特别去关注,也多多少少看了一些文字介绍,多数都是对使用可视化布局拖拽,个人对拖拽一直不看好,直到前端时间看到该文: 解析ConstraintL...

SuShine
9分钟前
0
0
开源 java CMS - FreeCMS2.8 数据对象 report

项目地址:http://www.freeteam.cn/ report 在使用申报相关标签时,标签会封装report供页面调用。 属性 说明 id id name 项目名称 unit 申报单位 filename 申报文件名称 filenum 申报文件文号...

freeteam
11分钟前
0
0
MySQL配置主从复制

1.master配置my.ini添加: log-bin=mysql-bin//开启二进制日志server-id=1innodb_flush_log_at_trx_commit=1//每次事务的结束都会触发Log Thread 将log buffer 中的数据写入文件并通知文...

ty淡然
12分钟前
0
0
在vs2017中将.obj链接到.lib中

项目属性-库管理器-常规-附加依赖项-"your.obj;....."

simpower
17分钟前
0
0
同一数据库,从这个表迁移到另外一张表

要求: 1、同一个数据库 2、两张表结构相同 insert into t_walk_user_step_history(user_id,city_id,steps,prop_steps,today_praise,today_date,insert_time)selectuser_id,city_id,st......

就叫程舰
18分钟前
0
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部