EventBus学习笔记

来源:互联网 发布:windows更新出现问题 编辑:程序博客网 时间:2024/05/18 03:59

以下是在github上EventBus的关于线程模式(ThreadMode)的说明文档,英语看着蛋疼,索性整页试着翻译了一下,英语水平有限,包涵。

原文链接:http://greenrobot.org/eventbus/documentation/delivery-threads-threadmode/

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)

线程模式:POSTING

使用这个模式注解的订阅者收到事件后将在分发此事件的原线程中执行接下来的代码。这个是默认的线程模式。一旦事件被分发,所有的订阅者将同步接收此事件,此线程模式因为是在原线程中执行方法,所以完全避免了线程的转换消耗,是所有模式中最省资源的。因此如果需要不依赖主线程快速完成简单的线程任务的话,推荐使用这个模式。注意,使用这个模式的话需要注意尽量不要进行耗时操作,以避免在主线程中执行的时候导致程序无响应。


@Subscribe(threadMode= ThreadMode.POSTING)// ThreadMode is optional here
publicvoid 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:

线程模式:MAIN
使用这个模式注解的订阅者收到事件后将在主线程(也称UI线程)中执行。如果分发此事件的线程是主线程的话,事件的handler方法将直接被唤醒(事件分发接收的同步情况和POSTING模式一样)。注意,使用这个模式的话需要注意不要进行耗时操作,以避免在主线程中执行的时候导致程序无响应。

// Called in Android UI's main thread
@Subscribe(threadMode= ThreadMode.MAIN)
publicvoid 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.

线程模式:BACKGROUND
使用这个模式注解的订阅者收到事件后将在后台线程执行。如果分发事件的原线程不是主线程的话,代码将在此原线程中执行,如果是主线程的话,EventBus将开一条独立的线程继续执行代码。使用此模式应注意尽量不要进行耗时操作。

// Called in the background thread
@Subscribe(threadMode= ThreadMode.BACKGROUND)
publicvoid 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.

线程模式:ASYNC
使用这个模式注解的订阅者收到事件后将在一条独立的线程中执行,这条线程将始终独立在主线程和分发事件的原线程之外。在此模式下,分发的事件不会等待handler的响应直到线程结束,使用者如果想要执行比较耗时的操作比如网络请求等建议使用此线程模式。为了避免引起太多的耗时操作堵塞程序,EventBus同时限制了核心线程数量,并且通过线程池循环使用已经执行完异步事件而空闲下来的线程来提高执行效率。









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