您的位置:首页 > 移动开发 > Android开发

eventbus/documentation/delivery-threads-threadmode

2016-04-07 00:00 423 查看
摘要: http://greenrobot.org/eventbus/documentation/delivery-threads-threadmode/

Delivery Threads (ThreadMode)

Contents [hide]

1 ThreadMode: POSTING

2 ThreadMode: MAIN

3 ThreadMode: BACKGROUND

4 ThreadMode: ASYNC

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);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android Libs EventBus