Android development art Exploration Study Notes (10) and android art Exploration
Chapter 10 message mechanism of Android
A frequently asked question during the interview: How handler transfers messages in sub-thread and main thread. This question can be answered accurately by understanding the message mechanism of Android.
Android message mechanism mainly refers to the Handler operation mechanism. Handler operation requires the support of the underlying MessageQueue and logoff. You need to understand two concepts:
MessageQueue: A Message Queue stores a group of messages through the data structure of a single-chain table, and provides insertion and deletion in the form of a queue.
Logoff: Message loop. It queries whether MessageQueue has a new message in an infinite loop. If yes, It processes the message. Otherwise, it waits.
To use Handler, you must create a lo for the thread where the Handler is located. When the main thread is created, the Looper is automatically initialized, and the sub-thread has no Looper by default, this is why Handler can be used by the main thread by default, but cannot be used by the Child thread by default.
Android provides Handler mainly to solve the problem that the UI cannot be accessed in sub-thread. The reason why the UI cannot be accessed in the Child thread is mainly because the control is not thread-safe, because multiple threads access the control may cause the UI control to be in an unexpected state.
Shows the Handler, MessageQueue, and logoff work projects:
First, Handler uses the post method to deliver a message to a logoff in handler by a Runnable or send method. In fact, the post method is also completed by the send method. Therefore, the send method is used as an example, after the send method is called, it will call the enqueueMessage method in MessageQueue to put the message into MessageQueue. At this time, logoff finds that a new message exists in MessageQueue and starts to process the message, in the end, the handleMessage method in Handler will be called, because logoff is running in the thread where the Handler is created, so that the message is transmitted in the subthread and main thread.
MessageQueue mainly includes two operations: insert (enqueueMessage) and read (next). The read operation is accompanied by the delete operation. The enqueueMessage method inserts a message into the message queue. The next method extracts a message from the message queue and removes it from the message queue.
The main function of logoff is to constantly check whether there are new messages from MessageQueue. If not, it will always be blocked.
To use Handler in a child thread, you must first create a Looper for the Child thread and use Looper. preper () can create a Looper for the current thread and then use Looper. loop () to enable the message loop. after use, you need to call logoff. to terminate the message loop. Otherwise, the subthread will remain in the waiting state because of an endless loop in the logoff, that is, the thread cannot end.