Message mechanism of Android

1、 Introduction

The message mechanism of Android mainly refers to the operation mechanism of handler. So what is the operation mechanism of handler? Generally speaking, the handler is used to put the message of the child thread into the messagequeue of the main thread and use it in the main thread.

2、 Learning content

To learn the message mechanism of Android, we need to understand the following contents first.

Usually, we contact mostly handlers and messages. Today, let's have an in-depth understanding of them.

3、 Code explanation

Generally speaking, we use handler in this way

xxHandler.sendEmptyMessage(xxx);

Of course, there are other representations, but when we go deep into the source code, we will find that they all call a method in the end

Sendmessageattime() method, but this is still not the end. We can see the last enqueuemessage (queue, uptimemillis); Literally insert a message, then the question comes, where is the message inserted.

Entering the source code, we found that we need to know a new class messagequeue.

Although we generally call it message queue, through research, we have issued that it is actually a single linked list data structure, and our operations on it are mainly insertion and reading.

Look at code 33-44. After learning the data structure, we can easily see that this is the operation of inserting the end of a single linked list.

In this way, we can see that the essence of our send method is to insert such a message into the messagequeue. Then another problem arises. How should we deal with this message.

We cannot deal with messages without an important, looper. So what role does it play in the message mechanism?

Looper plays the role of message loop. Specifically, it will constantly check whether there are new messages from the messagequeue. If there are new messages, it will be processed immediately, otherwise it is known that the block is there. Now let's take a look at its code implementation.

The first is the construction method

As you can see, it saves the current thread object. Let's go on

Looper has two important methods in the process of creating a new thread: looper. Prepare () looper. Loop

Let's look at the prepare () method first

Eh, we can see that there is another ThreadLocal class. Let's have a brief look at its features, the set() and get() methods.

Firstly, ThreadLocal is a data storage class within a thread. It can store data in the specified thread. After data storage, the stored data can be obtained only in the specified thread, but not for other threads. In short. Apply a column:

After the above code is run, we will find that the value of each thread is different, even if they access a ThreadLocal object.

Then we will analyze the source code later and why it is different. Now let's jump back to the prepare () method and paste the source code of the loop () method

First, use the loop () method to get the loop of this thread. If no exception is thrown. Then get the new messagequeue. Here we need to supplement the next () method of messagequeue.

From 24-30, we can see that he traversed the whole queue and found MSG. If MSG is null, we can see 50. He put nextpolltimeoutmillis = - 1; It is actually waiting for the nativewake of enqueuemessage to wake up. The deeper source code involves the native layer code. If you are interested, you can study it. Simply put, the next () method will return this message when there is a message. If there is no message, it will be blocked here.

Let's go back to the loop () method 27msg. Target. Dispatchmessage (MSG); Let's look at the code

Msg.target is actually the Handler that sends the message. We can see that it gives msg to dispatchMessage (), and finally calls our familiar method handleMessage (MSG).

3、 Summary

So far, we have learned about the message mechanism process of Android, but it actually involves deep native layer methods

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>