Principle of handler Mechanism

Source: Internet
Author: User

Principle of handler mechanism (conversion) and principle of handler Mechanism

Handler Overview

Andriod provides Handler and logoff to satisfy inter-thread communication. Handler first-in-first-out principle. The logoff class is used to manage MessageExchange between objects in a specific thread ).

1) Logoff: A thread can generate a logoff object to manage MessageQueue (Message Queue) in this thread ).
2) Handler: You can construct a Handler object to communicate with logoff, so as to push new messages to MessageQueue; or receive messages sent by logoff from Message Queue.
3) Message Queue: used to store messages placed by threads.

4) thread: UIthread is usually the main thread, and Android will create a MessageQueue for it when starting the program.

 

1. Handler creates a message Each message needs to be processed by the specified Handler. You can use the Handler to create a message. The message pool is introduced in the Android message mechanism. When creating a message, Handler first queries whether a message exists in the message pool. If a message exists, the Handler retrieves the message from the message pool directly. If no message exists, the Handler reinitializes a message instance. The advantage of using the message pool is that when a message is not used, it is not recycled as garbage, but put into the message pool for use when the next Handler creates a message. The message pool improves message object reuse and reduces the number of garbage collection times. Message creation process.

 

 

2. Handler sends messages

When the UI main thread initializes the first Handler, it creates a logoff through ThreadLocal, which corresponds to the UI main thread one by one. ThreadLocal is used to ensure that each thread creates only one logoff. When other Handler is initialized, The logoff created by the first Handler is obtained directly. A Message Queue MessageQueue is created during logoff initialization. At this point, the relationship between the main thread, message loop, and message queue is.

Handler, logoff, MessageQueue initialization process:

Hander holds a reference to MessageQueue and logoff in the message queue of the UI main thread. The subthread can send messages to MessageQueue in the message queue of the UI thread through Handler.

 

3. Handler processes messages

The UI main thread uses logoff to query the Message Queue UI_MQ cyclically. When a message is found to exist, the message is retrieved from the message queue. First, analyze the message, determine the Handler corresponding to the message through the message parameters, and then distribute the message to the specified Handler for processing.

The process in which the sub-thread communicates with the UI main thread through Handler and logoff.

 

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.