Detailed explanation of handler of Android thread message mechanism

Android thread message mechanism is mainly composed of handler, looper, message and messagequeue. Usually, in development, we often notify the main thread in the sub thread to update. In fact, the driver of the whole Android life cycle is implemented through the handler (activitythread. H).

First, let's introduce the functions of these four classes:

Handler: the sender of the message. Responsible for sending message messages to messagequeue. And realize the callback processing of messages through runnable, callback or handlemessage()

Looper: it is the loop processor of the message. It is responsible for fetching the message object from the messagequeue for processing. (looper contains a reference to messagequeue)

Message: it is a message carrier and refers to the handler through the target. Business logic data is contained through objects. Where messagepool is the message pool, which is used to reclaim the information of idle message objects.

Messagequeue: message queue, which is responsible for maintaining message objects to be processed.

Through the above figure, we can clearly know their role and relationship. Next, we analyze how this relationship is established from the perspective of source code.

Other construction methods of handler can be viewed by ourselves. Through this construction method, we know that handler holds the reference of messagequeue. So you can easily add messages to the queue.

Through the source code, we find that SendMessage - > sendmessagedelayed - > sendmessageattime - > enqueuemessage

All messages are added to the messagequeue through enqueuemessage.

Next, let's look at how message is constructed. Through the construction method of message.

We can see that the message is obtained from the message pool spool through the static method of obtain. In this way, message reuse can be achieved.

There is an overloaded method in which m.target = h; This code is very important to find the target handler of the message later for processing.

Next, let's look at looper. We know that looper enters the loop through looper.loop, and the loop is driven by the run method of the thread.

First of all, we know that we didn't create looper when we created the handler. Where did looper come from?

Look at looper. Myloop()

ThreadLocal is a class in which threads create thread local variables. Indicates that this variable belongs only to the current thread.

We see that the method of sthreadlocal. Get () actually takes the looper object in the current thread.

So where is the looper of our main thread created? We clearly know that if you create a handler call in a child thread, you need to use the looper. Prepare method.

We see that in this method, if there is no looper object in this thread, a looper object is created. Next, we see a familiar method in the source code.

This method separately creates a smainlooper for the looper of the main thread. Where is this preparemainlooper called?

Higher than the reference point, we found it in the activitythread. Main () method

Activitythread. Main () is the entry method of the program. In this way, it is very clear that the looper of the main thread has been created and looped during the startup of the program.

So how to call looper correctly if it is created in a child thread?

Next, we need to look at the execution method of loop. Loop ()

Finally, let's look at the processing method of dispatchmessage.

We can see that dispatchmessage optimizes the processing of msg.callback, then the implemented callback interface, and finally the handlemessage method.

Key notes:

1. When the handler is instantiated, it holds the reference of looper. It is associated with the handler through ThreadLocal.

2. During the instantiation of message, the handler reference is held through the target.

3. Usually, a thread corresponds to a looper. A looper can belong to multiple handlers.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support 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
分享
二维码
< <上一篇
下一篇>>