The relationship between message, Handler, message Queue, Looper

Source: Internet
Author: User

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

1. Message

A message is an information that can be understood as a communication between threads. Processing a data background thread requires updating the UI, and you can send a message containing some data to the UI thread.

2, Handler

Handler is the processor, is the main processor of the message, responsible for sending the message, message content execution processing. A background thread is a SendMessage (Message) by passing in a handler object reference. With handler, you need to implement the Handlemessage (Message) method of the class.

3. Message Queue

Message queue, which is used to store messages published through Handler, is executed according to the FIFO principle. Each message queue will have a corresponding handler. Handler sends messages to message queue in two ways: SendMessage or post. Both messages are inserted at the end of the message queue and are executed according to the FIFO principle. However, the messages sent by these two methods are slightly different: sending a Message object via SendMessage is handled by the handler Handlemessage () method, and the Post method sends a Runnable object. It will execute itself.

4, Looper

Looper is the steward of each line thread message queue. Android does not have a global message queue, and Android automatically creates a message queue for the main thread (the UI thread), but the message queue is not established on the thread line. So calling Looper.getmainlooper () gets the looper of the main thread is not NULL, but calling Looper.mylooper () gets the looper of the current thread to be null.

Borrow a flowchart from someone else.

The relationship between message, Handler, message Queue, Looper

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.