Reputation: 7466
With libraries like Otto and EventBus I wonder whether it still makes sense to use Handler:
A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue.
How can handlers be used in addition to event bus libraries? I recon it is sufficient to use vanilla threads and send messages directly over the event bus or am I missing something here?
Upvotes: 2
Views: 1458
Reputation: 152787
Usually you use event bus libraries and handlers for different problems.
Event bus libraries allow information consumers to subscribe to particular events and producers to publish them, without the publisher and subscriber components needing to know really anything about each other. More than one consumer can subscribe to an event; more than one producer may publish it. The model is many-to-many.
Handlers on the other hand are one-to-one. You send a Message or post a Runnable and it gets processed only by the target Handler once, unless removed before execution. The core purpose of handlers is scheduling work on a thread, as mentioned in the documentation you quoted.
You can use Handlers and Messages to implement an event bus. For example, have the bus be a collection of Handlers and the events Messages. The event messages get sent to those handlers that have registered themselves as subscribers to a particular event type.
Upvotes: 10