What I know from EventBus
阿新 • • 發佈:2019-01-09
ThreadMode
onEvent(EventType event)
- PostThread is default ThreadMode.
- Subscriber will be called in the same thread which is posting the event.
- Subscriber will be called in the main thread.
- If the posting thread is main thread, onEventMainThread(EventType) will be called directly; If not, the Event will be sent to main thread by the handler, then call the onEventMainThread(EventType) in handleMesage(Message).
- onEventMainThread(EventType) must return quickly to avoid blocking the main thread.
- Subscriber will be called in a background thread.
- onEventBackgroundThread(EventType) will be called directly if posting thread is not main thread. If posting thread is the main thread, EventBus uses a single background thread, that will deliver all its events sequentially.
- onEventBackgroundThread(EventType) must return quickly to avoid blocking the background thread.
- Subscriber will be called in a separate thread.
- This is not relative to the posting thread or the main thread.
- Event handler methods should use this mode if their execution might take some time(network request).
- The code use some technology to avoid concurrency, like double check in single instance, CopyOnWriteArrayList, synchronized.
- Reuse the PendingPost in pendingPostPool.
- How to use ExecutorService to schedule the thread and avoid concurrency.
- Learn Source Code also help us understand the origin of the library.