Relationship between message, Handler, message Queue, Looper in a single-threaded model

Source: Internet
Author: User
Tags message queue

Message, a carrier of information, used to pass data to handler.

Handler (Handler processor, is the main processor of the message, responsible for the delivery of the message, the execution of the message content processing)
Sends and processes the message and Runable objects, which are associated with the MessageQueue of a thread. Each thread instance is associated with a separate thread and the MessageQueue of that thread.
Handler is bound together with the thread that created it, and the message and Runable objects are passed to MessageQueue, and Handler is responsible for executing them when they leave the MessageQueue.

The handler has two main uses:
(1) Determine the execution of one or more message and runnable objects at a later point in time.
(2) Some of the actions to be performed are queued in other threads (not Handler bound threads).

Post (Runnable): Runnable executes on handler bound thread, that is, does not create a new thread.
Postattime (Runnable,long):
Postdelayed (Runnable,long):
Sendemptymessage (int):
SendMessage (Message):
Sendmessageattime (Message,long):
Sendmessagedelayed (Message,long):

SendMessage This action queues the Message object, which contains some information, and handler's Hanlermessage (message) handles the message.
Handlermessage (Message) must be overridden by the Handler subclass.

Post This action puts the Runnable object into the Messagequeue,messagequeue when you receive these messages, execute them in a certain sort of order.

When posting or sending to a hanler, you can have three behaviors: when the MessageQueue is ready to process, define a delay time, define an exact time to process. The latter two allow you to implement Timeout,tick, and time-based behavior.

When a new process is created, the main thread comes with a MessageQueue, which manages the top-level app object and the form created by the main thread.

MessageQueue: (Message queue, used to store messages published through Handler, followed by FIFO execution.) )
The underlying class that contains the message list, Looper is responsible for distributing the message. The message is not added directly to the MessageQueue, but is associated to the Looper by Messagequeue.idlehandler
You can get MessageQueue from the current thread by Looper.myqueue ()

Each message queue will have a corresponding Handler. Handler will pass to MessageQueue.
Two ways to send a message: SendMessage or post. Both messages will be inserted at the end of the message queue and
Follow the FIFO execution. However, the messages sent by these two methods differ slightly in the way they are executed: by sendMessage
Sends a Message object, which is handled by the Handler handlemessage () function, and by the post side
Method sends a Runnable object, it executes itself.

Looper:
The Looper class is used to execute a message loop in a thread. Call prepare () in the thread to create a Looper, and then use Loop () to process the messages until the loop terminates.
(Looper is a bridge of communication between Handler and message queues, and the program component first passes the message through Handler to
Looper,looper the message into the queue, it can also get messages in MessageQueue, generate message queue, pass to handler)
Most of the interactions with message loops are through handler.

The following is a typical threading implementation with Looper.
Class Looperthread extends Thread {
Public Handler Mhandler;
public void Run () {
Looper.prepare ();
Mhandler = new Handler () {
public void Handlemessage (Message msg) {
Process incoming messages here
}
};
Looper.loop ();
}
}

Relationship between message, Handler, message Queue, Looper in a single-threaded model

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.