Summary of Android message mechanism problems

The message mechanism of Android is almost a topic that must be asked in the interview. Of course, it is not because of the interview. More importantly, it is essential and plays an important role in the development of Android, so it is very necessary to understand it. Let's talk about the most basic things.

Looper

effect:

Associate thread

Loop fetch message

1. Can looper be instantiated directly?

The looper constructor is private and does two things

Create a messagequeue

Get the corresponding thread

2. Can a thread correspond to multiple loppers?

No, a thread corresponds to a looper object. Ensure that a thread has only one looper corresponding to it through ThreadLocal. If looper is called multiple times prepare(); A runtime exception is thrown.

3. Looper is an infinite loop. Will it block?

Yes, when a loop is opened, it is an endless loop. Take messages from the messagequeue and process messages, but it is also possible to exit. Exit the loop when there is no message.

4. Looper can be called again Preparemainlooper?

No, looper Preparemainlooper finally calls prepare(), the same as 2

5. When was mainlooper created?

Mainloop is created when the activity is started to create an activitythread (not a thread), so it cannot be created multiple times.

Handler

effect:

Send message to messagequeue

Processing messages

1. How is the handler associated with looper and messagequeue?

We know that a looper corresponds to a thread, and a looper contains a messagequeue. When we create a handler, we will take out the corresponding looper from the current thread, and then let us take out the messagequeue from the looper.

Message

Single linked list structure.

effect:

Data carrier

1. How are messages reused?

From the global message pool (linked list structure)

2. Why can message be delivered?

In Android, if you want to transfer objects, you can either implement serializable or Parcelable. Here, you implement the Parcelable interface.

Public final class message implements negotiable {/ / omitted}

3. How to associate with handler?

We know that in the message transmission mechanism, the handler acts as a "courier", so how does he relate to the "goods" - message? In fact, message has a member variable target, and its type is handler,

When we send a message through the handler, we will eventually assign this to the target, that is, the current handler.

The other is through message Obtain(), and the multiplexed message obtained will also be assigned a value.

One more word, handler Obtainmessage() calls message Obtain()。

Summary:

Through a series of inclusion relationships, looper, handler, message and messagequeue are finally associated to form a closed message loop.

puzzled

I've been looking at this knowledge recently, but my ability is limited. There are still many puzzles. If there are errors or you understand the following questions, please contact me fvaryu@qq.com , I would like to exchange and study with you, thank you

1. Where is the spool in the message initialized? Why message Will exceptions not be thrown in obtain()?

2. Activitythread is not a thread. Why can a looper be created? When will the main thread be created?

3. Why can serialized objects be passed? About binder?

4. Messagequeue corresponds to nativemessagequeue. How to implement it?

5、Loop. Loop (), will you exit? What is the exit time? If it exits, will the main thread also exit?

The above is the data sorting of Android message mechanism. We will continue to supplement relevant data in the future. Thank you for your support for this site

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
分享
二维码
< <上一篇
下一篇>>