Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the principle of Handler

Shulou Source: shulou.com Published: 2022-06-02 03:44:34 10月13日 Update

This article shows you what the Handler Principle is. It is concise and easy to understand. It will definitely make your eyes shine. I hope you can gain something from the detailed introduction of this article.

What is the principle of Handler:

Handler encapsulates the message sending (mainly including who the message is sent to sendToTarget, sent to yourself by default)

Looper

It contains a message queue inside, that is, MessageQueue, and all messages sent by Handler go to this message queue.

Looper.Looper method, is an endless loop, constantly get messages from MessageQueue, if there is a message to process the message, no message to block

MessageQueue: A message queue where messages can be added and processed.

Handler is also very simple, the internal will be associated with the Looper, that is to say, in the Handler can find the Looper, find the Looper will find the MessageQueen, send messages in the Handler, in fact, send messages to the MessageQueen queue

Summary: handler copies and sends messages, Looper is responsible for receiving messages sent by Handler and directly transmitting messages to handler itself, MessageQueue is a container for storing messages

Threadlocal is used to store some variable information in the thread, such as set(store variables in Threadlocal), get(get variables from Threadlocal)

As you can see from the Handler source code, creating a Handler automatically creates a Looper object.

From the Looper source code, you can see that Looper contains messageQueen member variables. In fact, the purpose of creating Looper is to obtain MessageQueen.

As you can see from the follow-up source code of sendEmptyMessage of Handler, the mechanism of handler sending messages is implemented through messagequeue.

messageQueue is implemented by sending messageQueue to Handler via an endless loop in Looper.loop(); method

The above picture is a screenshot from Mu class network to understand the principle of Handler through illustration.

What is the Handler Principle? Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to the industry information channel.

Tags: Messages that is principles variables methods queues that is content skills source code knowledge processing storage looping concise concise above constantly that is to say information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Technology Docker vpn MariaDB