Android Handler Mechanism principle

Source: Internet
Author: User

Andriod provides handler and Looper to meet the communication between threads. Handler first-out principle. The Looper class is used to manage message exchange (Messageexchange) between objects within a particular thread.

1) Looper: A thread can produce a Looper object that manages the MessageQueue (Message Queuing) of this line thread.
2) Handler: You can construct a Handler object to communicate with Looper in order to push a new message into the MessageQueue, or to receive a message sent by Looper from the message queue.
3) Message queue (Message Queuing): Used to hold messages placed by the thread.

4) Thread: Uithread is usually the main thread, and Android initiates a program to create a MessageQueue for it.

1.Handler Creating messages each message needs to be handled by the specified handler, and the message can be created by handler to complete this function. The message pool is introduced in the Android messaging mechanism. Handler when a message is created, it first queries whether a message exists in the message pool, if one is obtained directly from the message pool, and if not, reinitialize a message instance. The advantage of using a message pool is that when a message is not being used, it is not garbage collected, but instead is put into a message pool that can be used the next time handler creates a message. The message pool improves the reuse of message objects and reduces the number of system garbage collections. The creation process of the message.

2.Handler sending messages

When the UI main thread initializes the first handler, a looper is created through threadlocal, which corresponds to the UI main thread one by one. The purpose of using threadlocal is to ensure that each thread creates only one looper. After the other handler initialization, the first handler created Looper is obtained directly. A message queue MessageQueue is created when the Looper is initialized. At this point, the relationship between the main thread, message loop, and message queue is 1:1:1.

Handler, Looper, MessageQueue initialization process:

Hander holds a reference to the UI main thread message queue MessageQueue and message loop Looper, which can be sent by handler to Message Queuing MessageQueue of the UI thread.

3.Handler processing messages

The main thread of the UI queries the message queue UI_MQ through Looper, and the message is removed from the message queue when a message is found to exist. The message is parsed first, the message's corresponding handler is judged by its parameters, and the message is distributed to the specified handler for processing.

The process by which a child thread communicates through handler, Looper, and the UI main thread.

Android Handler mechanism principle (RPM)

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.