Parse Android Message Processing Mechanism: handler/thread/logoff & messagequeue

Source: Internet
Author: User

Parse Android Message Processing Mechanism

-- Handler/thread/logoff & messagequeue

 

Tian haili @ csdn

 

Keywords: Android message handlerthread low.uml

This article explains how Android uses handler/thread/logoff and messagequeue to implement the internal implementation of the message mechanism. After learning about its internal implementation mechanism, you can easily solve any problems you encounter when using them.

Android uses the corresponding message distribution/processing of logoff in the handlerthread thread to combine with message sending in other threads to implement a complete message processing mechanism. This article first introduces the relationship between participants in the message processing process, and then explains the whole process of Message Processing Based on the actual instance of wifiservice message processing. Finally, it also describes the thread view, review the Thread Synchronization Model.

I. handlerthread/lofter/messagequeue past and present

Is the relationship between handlerthread, logoff, and messagequeue extracted from froyo. They are defined in package Android. OS.

Figure 1: handlerthread/logoff/messagequeue instance relationship

HandlerthreadIs a thread inherited from the thread. It retains the reference to the logoff instance, but there is no way to instantiate handlerthread, logoff, and messagequeue. This will be done only after handlerthread/logoff/messagequeue is instantiated.

You can see at a glance,LogoffThe constructor is private and cannot be directly instantiated by the outside world. to instantiate the constructor, you only need to use Logoff: Prepare (). In this way, handlerthread establishes a correspondence with logoff. If you can't wait to create a project, let's take a look at the next section.

 

2. handlerthread/logoff/messagequeue instantiation

The previous section only looked at the static view relationships between handlerthread, logoff, and messagequeue. How do they instantiate them? It also looked at the dynamic instance initialization process.

Figure 2: handlerthread/logoff instantiation


  • HandlerthreadIs a thread. After it is instantiated and executed start () [sequence 1 & 2], its run () method will execute [sequence 3] in its own thread space.
  • As mentioned in the previous sectionLogoffThe instantiation of is implemented through Logoff: Prepare. The figure shows that Logoff: Prepare () is exactly in handlerthread: Run () and instantiate [sequence 4 & 5.
  • WhileMessagequeueIt is instantiated in logoff's private constructor logoff.

To sum up, handlerthread is an instance explicitly created through new, and The logoff bound with it is instantiated during the execution of handlerthread, the corresponding messagequeue is also instantiated in this process.

 

3. Logoff: The loop distributes and processes messages.

The previous section focuses on the instantiation process of handlerthread/logoff, which occurs in thread: Run (). This is also the key to thread running. From figure 2, we also see Logoff: loop () the core of message processing is implemented here. Let's take a look at what it actually does.

Figure 3: Logoff: loop () Processing


All sequences in the figure occur in an infinite loop body while (true.

  • Mqueue: messagequeue is a reference retained by logoff. The next message to be processed in messagequeue is obtained through its next () [sequence 1]. If no corresponding message exists in this process, the thread that executes it will use this. wait () releases its own messagequeue object lock and waits.
  • Once a piece of news arrives, when the sequence is 2 bytes, looperwill use the handler(msg.tar get) of the original messageto distribute and process messages [sequence 3 & 4]. After the message is processed, [sequence 5] will be recycled.

 

4. Handler implements specific message processing

Handler is displayed in the order of Logoff: loop () message processing, and the participant in message distribution processing. The following describes how wifihandler participates in message sending, distribution, and processing.

4.1 handler instantiation

Wifiservice defines the following wifihandler.

Figure 4. handler of message sending, distribution, and handling


The handler constructor requires logoff. From the above analysis, we know that logoff is instantiated during handlerthread execution. How can a logoff instance obtain it? See Figure 1 & Figure 2. We know that handlerthread retains the logoff application mlogoff and can get it from the outside through getlogoff. Handler's mqueue: messagequeue can be obtained through mlooper. mqueue.

Therefore, the handlerthread and wifihandler of wifi can be instantiated as follows:

HandlerThread wifiThread = new HandlerThread("WifiService");wifiThread.start();mWifiHandler = new WifiHandler(wifiThread.getLooper());

 

4.2 message sending

Figure 5. Message sending

Message: obtain () can be used to establish the relationship between message and target, handler, and what, and get a message MSG [sequence 1 & 2]; and then use MSG. sendtotarget () can be used to send messages using handler [sequence 3 & 4]; through a series of calls, messagequeue: enqueuemessage () put the message on the mmessages [sequence 7] and use this. the notify () Notification is waiting for the thread of the new message to re-possess the messagequeue object lock and process the message.

Figure 6 Relationship Between messagequeue and message

At this point, the message sending, distribution, and processing are basically completed.

4.3 Message Processing

To process a specific message, we can see from sequence 4 in Figure 3 that the final message processing of the entire framework is completed through handler: handlemessager (MSG: Message. Therefore, if you have a specific message to be processed, you only need to override handler's handlemessage (MSG: Message) method and add your own specific processing method to the message.

To process a message, you can see what is in the message attribute.

Figure 7. Message attributes

Pay attention to what is the public attribute to differentiate specific messages. The parameter arg1 and arg2 can be included.

 

5. Review 5.1 Application Guide

The whole process of message processing is introduced above. These may be complicated for users who only send and process messages. Here we will look at how to use them from the perspective of users.

Actually, Handler's implementation of wifiservice in specific message processing is a typical application scenario.

Implement a subclass of handler and the override handlemessage () method, that is, implement the handlemessage () function of message processing (). Create handlerthread and hanlder inheritance class instances respectively. In this way, messages can be sent as in 4.2, and message processing is performed in handlemessage.

From the perspective of the application, logoff and messagequeue are the internal mechanism of message processing, so you can ignore the implementation details (the only thing you need to know is that when handler is instantiated, you need to use handlerthread to obtain the logoff instance, which can be passed to handler ).

5.2 thread angle

Next, let's look at the threads involved in message processing and the synchronization between these threads.

Obviously, the thread handlerthread is involved here, and logoff: loop () is executed in the run () method of handlerthread, that is, in handlerthread, that is to say, the distribution, processing, and execution of messages are in the thread context of handlerthread. In addition, at least one thread exists, that is, thread B of handlerthread is created, and the thread C, B, and C that execute message sending may be the same thread.

The message is sent in another thread. The synchronization operation is performed only when multiple threads exist. You can pay attention to the Java primitive wait ()/notify () for Thread Synchronization in steps 1, 3 and 5 ().

 

Conclusion

The original intention of writing this article is not to write it, but to study WiFi implementation in Android. Various specific operations to achieve WiFi are implemented through messages, I want to discuss about the android message processing mechanism, so that I can use rose to draw a picture and wait until the process of message sending, receiving, and processing is complete, looking back at the entire UML diagram, this is not a complete message processing mechanism!

After so many years of work, let's look back at what we have accumulated. Let's look at my blog 5 or 6 years ago and find that there was something left behind at that time, and when I wrote this article, when reviewing the thread synchronization mechanism in Java, I saw what I was writing at that time and found that if it was something I understood at that time and expressed in my own fields, I can see it clearly when I look back, several UML diagrams can solve the problem.

 

References

Google Android source froyo (2.2) git: // android.git.kernel.org/platform/manifest.git

Tian haili parsing JVM thread synchronization mechanism http://blog.csdn.net/thl789/article/details/566494

 

Correction record

Modify the title, abstract, and keywords

The main participants of message sending and distribution processing are logoff, handler, and thread execution. Many application scenarios can be completed with their participation. handlerthread is a special case of Message Processing during wifiservice processing. Therefore, handlerthread is removed from these key points.

(This article can be reproduced freely, but please provide the original article link:Http://blog.csdn.net/thl789/article/details/6601558).

********* **********************

 

 

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.