5 Roles of the handler mechanism
In the last Android processing time-consuming task _anr a simple introduction to the use of a handler, let's take a look at his rationale.
Handler: Handler, responsible for sending and processing the message. When using handler, you need to implement the Handlemessage (Message msg) method to process a specific message, such as updating the UI.
Message: Messages, carrying the information and tasks to be transmitted, are the carriers of the handler mechanism.
MessageQueue: Message Queuing, which holds messages sent by handler and executes according to FIFO rules. Storing a message is not a meaningful save, but instead concatenates the message in a linked list, waiting for the looper to be extracted.
Looper: The message pump continuously extracts message execution from the MessageQueue. Therefore, a MessageQueue needs a looper.
Thread: Threads that are responsible for dispatching the entire message loop, that is, the execution site of the message loop.
Looper and MessageQueue one by one correspond, creating a looper will create a MessageQueue at the same time. And handler's relationship with them is simply a matter of aggregation,
That is, Handler will refer to the specific looper and MessageQueue of the current line thread. In this way, multiple handler can share the same looper and MessageQueue.
Of course, these handler are also running in the same thread. I have specifically drawn the following diagram, I think this diagram has been able to clear and concise explanation of their relationship.
a simple example
Class Looperthread extends Thread {public Handler Mhandler; public void Run () { looper.prepare ();//handler default constructor with no parameters: New Handler (), which actually gets the message loop in the current thread through Looper.mylooper () Mhandler = new Handler () {public void Handlemessage (Message msg) { //process incoming messages here }
}; Looper.loop (); } }
By default, a thread does not have a message loop looper, it needs to call Looper.prepare () to create a looper for the thread, and creates a MessageQueue that calls Looper.loop () to make the message loop work. Takes a message from the message queue and processes the message. Looper.loop () actually opens a dead loop, extracting the message from the MessageQueue and assigning it continuously. So the code written after Looper.loop () will not be executed immediately, and after the call to Mhandler.getlooper (). Quit (), loop will not abort until the code can be run.
It is worth mentioning that many of the communication between threads in Android is related to handler:
Activity.runonuithread (Runnable)
View.post (Runnable)
View.postdelayed (Runnable, long)
All of the above three are actually related to Handler.post (Runnable).
Activity.runonuithread (Runnable)
If the current thread is a UI thread, the runnable executes immediately if the current thread is not the UI thread, and the handler post () method of the UI thread is called into the message queue of the UI thread.
View.post (Runnable)
View.post (Runnable) method. In the post (Runnable action) method, view obtains the handler of the current thread (that is, the UI thread) and then posts the action object into the handler. In handler, it wraps the action object passed in as a message (the callback of the message is the action) and then puts it into the message loop of the UI thread. When handler processes the message again, there is a branch (the one that is not explained) that is set for it to call the runnable run method directly. At this point, it has been routed to the UI thread, so we can update the UI with no worries.
Android Time consuming task _handler