I was just seeing the documentation of three methods which can be used to execute a piece of code in the UI thread while we are working in a worker thread. The methods are:
public final void runOnUIThread(Runnable action) - Runs the specified action on the UI thread. If the current thread is the UI thread, then the action is executed immediately. If the current thread is not the UI thread, the action is posted to the event queue of the UI thread
public boolean post(Runnable action) - Causes the Runnable to be added to the message queue. The runnable will be run on the user interface thread.
public boolean postDelayed(Runnable action, long delayMillis) - Causes the Runnable to be added to the message queue, to be run after the specified amount of time elapses. The runnable will be run on the user interface thread.
The first one posts the Runnable to the event queue of the UI thread, while the other two add the Runnable to the message queue. Please tell me the difference between the two?
My web search tell me that an event queue is simply a queue of events waiting to be executed by the thread. I am not clear about the message queue. MessageQueue is some class as well, is this related to that?
Thank you in advance.
The difference between events and messages is that events are used for intra-stack communication, whereas messages are used for inter-stack communication.
Once messages are written to a stream, they stay there. Conversely, with message queues or message buses, a message is removed from a queue and then passed to a service's consumer.
Events are messages As discussed above, in Message Driven systems, each component send items to a fixed recipient. In Event Driven systems, on the other hand, each component produces items of data with a fixed sender and shares them with any consumer.
An event queue is a repository where events from an application are held prior to being processed by a receiving program or system. Event queues are often used in the context of an enterprise messaging system.
This means that the messages in the queue are actually commands, which is suited towards imperative programming, and not an event, which is suited towards reactive programming. With queues, you generally execute the same logic in the same domain for every message on the queue
With an EventQueue this is not necessary. So message managing gives more control to the sender of the message. With an event queue all the sender can do is throw a request in the queue and hope for the best. That additional control provided by a MessageQueue comes with a small complexity penalty.
In essence, instead of having an Event result in a service yelling to a group of interested parties “something happened!”, a Message Queue takes an event or output that requires additional processing and adds it to a long queue.
Queues usually allow for some level of transaction when pulling a message off, to ensure that the desired action was executed, before the message gets removed. Not all queueing systems have the same functionality, but once a message has been processed, it gets removed from the queue.
A message queue and an event queue are very similar design patterns with one notable difference.
First off let's review the similarities. Both are asynchronous. They store notifications in FIFO order. Sending a notification enqueues the event/message and returns.
Later on the EventManager
/MessageManager
will dispatch all those Event
s/Message
s to the recipient objects. The difference lies in that with MessageQueue
s it's typical that the sender requires a response. With an EventQueue
this is not necessary.
So message managing gives more control to the sender of the message. With an event queue all the sender can do is throw a request in the queue and hope for the best. That additional control provided by a MessageQueue
comes with a small complexity penalty.
Choose the simplest data structure that you need for the job.
I think the two are synonymous. Events are indicated to the system using messages.
The real difference between the two methods is that one appends it to the queue immediately while the other delays it by the specified amount.
EDIT: More on messages
Messages are a way of communication between independent threads. In a way, it's a lot like the communication that takes place when you pull up a website in your browser: You send a message to the server detailing what exactly it is you want (GET www.stackoverflow.com, I will accept the following character encoding, do not track me, blablabla), which makes the server as the recipient of the message do something (retrieve content from the database, render the page, etc) and communicate the results of this back to you via a message.
How it works is like this: The thread has a Looper
attached to it. All it does is run forever in a continuous loop, on each iteration checking whether there are any messages in its message queue. If there aren't, it goes to the next cycle. If there are, it retrieves the first message to deal with it.
However, the looper itself doesn't know what any of the messages mean - it's just there for looping. Neither does the thread, which just provides the infrastructure for the looper to run in. What the looper does know, however, is who to go to for handling the message: One of its Handler
s. It passes the message to the handler, which can now go and do whatever it is that it needs to do to handle the message.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With