Android Asynchronous Message Processing (1)

Source: Internet
Author: User

1. Statement before writing an article: First, writing original labels in the article is a little far-fetched, essentially Reading Notes. On Saturday last week, I attended an exchange meeting of authors from the blog point of view. The editor sent me an android kernel book "Android Kernel Analysis" and read it back. It was great. First, the author has a wealth of knowledge and has a good understanding of android, which is a lot higher than many other Android editors. Second, the level is high and normal. Do you write a book? It is a bit level, but when I write a book, I am very careful, and the language is easy to understand, simply. This is very difficult. When I write my own books, I feel that knowing is one thing, expressing it clearly, and expressing it clearly is another thing. Finally, the series of articles are suitable for users who have a certain degree of knowledge about android. If they do not have an android Foundation, don't read them. It is a waste of time.

2. the significance of writing these articles: there is a lot of Kernel Analysis in the book. In fact, for application-layer developers, these things cannot be used. I just extract things for application development, then add something of your own.

 

What is asynchronous message processing:

For a common thread, the thread ends after the code in the run () method is executed. The Asynchronous Message Processing thread refers to the process in which a thread enters an infinite loop after being started. Each time it is executed, a message is retrieved from the Message Queue within the thread, and calls back the corresponding message processing function. After a message is executed, the loop continues. If the message queue is empty, the thread will pause (generally we call the sleep method) until there are new messages in the message queue.

 

Asynchronous Message Processing Features:

From the above description, we can see that asynchronous message processing is actually a kind of thread mechanism, but this mechanism is used in many places, and finally the term "Asynchronous Message Processing" is extracted separately.

 

Usage of asynchronous message processing:

Generally, if a task has the following two features, you can use the asynchronous message processing mechanism:

1. The task resident memory (in programming, the run () method is infinite loop), for example, the task used to process user events.

2. The task needs to perform different operations based on the externally transmitted messages.

 

General Asynchronous Message Processing Methods:

1. Each asynchronous thread contains a message queue to cache messages.

2. during thread execution, while (true) is used for an infinite loop. The loop body extracts messages from the message queue and calls back the corresponding message processing function based on the message source (from here we can see: asynchronous Message Processing, the specific processing of messages is not the responsibility of the asynchronous message processing mechanism, the asynchronous message processing mechanism is only responsible for forwarding messages to the processing function)

3. Other external threads can send messages to the Message Queue of this thread. Because two or more threads access the message queue, the read/write operations inside the message queue must be locked.

 

Implementation of Asynchronous Message Processing in Android:

There is one or more Handler objects in the thread. The External Thread sends an asynchronous message to the thread through the reference of the Handler object, and the message is added to the MessageQueue through the Handler object ). There is only one MessageQueue object in the thread. The run () method of the thread reads messages from MessageQueue and calls back the handleMessage () callback function in the Handler object to process messages.

 

Notes during compilation:

1. Because MessageQueue is an important part of the asynchronous message processing mechanism, you must confirm that MessageQueue has been created before sending a message. In android, the interface provided to application programmers to create MessageQueue is logoff. prepare ().

2. the asynchronous message processing mechanism is a loop thread, and the startup of the loop is controlled by the programmer. Therefore, you should start the loop before processing the message. In android, the startup loop interface provided to application programmers is logoff. loop ().

3. Careful friends may find that the above two steps are not involved when Handler is used to send and process messages in the activity. In fact, the system has helped us. Before creating an Activity, the system creates an asynchronous message processing thread for the Activity.

Author: "Feng Xiaowei"
 

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.