Handler message mechanism learning record

Schematic diagram:

Class structure diagram:

Timing diagram:

As can be seen from the schematic diagram, MessageQueue and child threads have a one-to-many relationship, and MessageQueue and Looper have a one-to-one relationship.

So how to do it, different sub-threads push messages to the message queue, and get the same message queue?

      Answer: Binding the MessageQueue and Looper, the child thread will get the Looper object, and then get the MessageQueue object.

When to get the Looper object?

      Answer: Looper has a one-to-one binding relationship with the main thread. If you want to get the Looper object in the main thread, the Handler must be instantiated in the main thread, so the Looper object should go when the Handler is instantiated. Obtain.

Why update the UI on the main thread?

      Answer: The system designer, in order to consider user experience, system performance

What should I do if the child thread wants to update the UI?

      Answer: Send a message to the main thread through the message mechanism, and let the main thread update the UI. This message mechanism is the Handler

Can Handler only be used to update the UI?

      Answer: Handler is actually mainly used for thread communication, that is, there may be communication between sub-threads and sub-threads. Then the looper initialization of the poller may appear in the sub-thread. How to ensure that the Looper object can be obtained correctly in the sub-thread, What about loop polling? The answer is ThreadLocal

What is ThreadLocal?

      Answer: ThreadLocal is not a Thread, but a local variable of Thread. ThreadLocal was born to solve the concurrency problem of multiple threads. It makes variables have independent copies in each thread, and there is no phenomenon that one thread reads variables and is modified by another thread.

Why finally return to the handleMessage() method to handle the secondary message?

      Answer: Because we are going to return to the handleMessage() method in the end, we first define a Handler reference target in the Message class, then assign msg.target = this in the sendMessage method of the Handler class, and then in the Looper polling method loop Get the Message object msg inside, and directly adjust (forward) the msg.target.dispatchMessage(msg) method to return to the handleMessage() method.

Why do msg.target = this (that is why Message needs a target)?

      Answer: The application of the principle of least knowledge in Handler is not only the sender of the message, but also the processor of the message. Only one Handler class can use the extremely complex message mechanism behind it.

The similarities and differences between Handler's post() and postDelayed() methods?

  1. The bottom layer is called sendMessageDelayed()->sendMessageAtTime()
  2. The time parameter passed in post() is 0
  3. The time parameter passed in postDelayed() is the required time interval.

Guess you like

Origin blog.csdn.net/m0_37670495/article/details/109305342