Android Threading message passing mechanism--looper,handler,message
Before introducing these concepts, let's look at the context in which these mechanisms are introduced.
For performance optimizations, Android UI operations are not thread-safe (if you don't know what thread safety is, you can read < Explore multi-process and multithreaded > Data Security and reentrant), which means that if multiple threads are working on a UI component at the same time, May cause thread safety issues. To solve this problem, Android has a simple rule: only allow the UI thread to modify the UI components in the activity. This UI thread is also commonly referred to as the main thread.
Here's a question: What if your Android program wants to download a picture on the Web (we don't consider using Android-provided components for the moment), and it shows up when it's downloaded? After Android3.0, the network request is not allowed to execute in the main thread. OK, I'll open a sub-thread to download it! However, after the download, the child thread can not manipulate the ImageView display picture, it is necessary after the download, the child thread to notify the main process to update the ImageView. This involves the communication mechanism between the threads. The Android.os.Handler class is used for communication between Android threads.
In fact, the communication between the threads is required to use several classes together, these classes are: Looper,handler,message. In fact, there is a message Queue (MQ), just encapsulated in the looper, we do not directly deal with MQ.
The looper--is responsible for looping out the message from MQ, and then throwing messages to the Hanlder Handlemessage method.
handler--is responsible for handling the message sent by Looper and sending messages to Looper. To override the Hanldemessage method when instantiating, process the corresponding message.
message--a message packet with a target field indicating which handler the message was sent to the thread.
Message queue that consists of message queue--messages packets.
Let's talk about how they work together. When your a thread needs to accept a message from another B-line Cheng, you need to create a handler,b thread in the a thread to send a message to the handler you created. Then a thread will be able to receive the message. However, it is important to note that when creating hanlder, you need to bind a looper. If you do not specify a Looper object to bind to when you create handler, the system defaults to binding the looper of the current thread to the handler. If your current thread does not create a looper, the system will make an error. That has classmates said, I usually in the UI line thread with handler when not created Looper also did not error Ah! That's because the system has already created Looper for the UI thread by default. If you create a child thread that also wants to have a handler, then you need to help create a looper for your strand enters upgradeable. Here's a piece of code to see how threads, Looper, handler, and message are going to work.
Class MyThread extends thread{
Public Handler Mhandler;
public void Run ()
{
Looper.prepare ();
Mhandler = new Handler ()
{
@Override
public void Handlemessage (Message msg)
{
Processing the message here
}
}
Looper.loop ();
}}
It is important to note that a thread can have only one Looper object. If you simply want to create two looper for a thread, that ... That... You can't actually do that! Because the Looper object is defined as threadlocal (if you do not understand threadlocal, you can check online). Ensure that a thread can have at most one looper. In fact, Looper can not be instantiated with new, because Looper's constructor is private, call Loop.prepare () to create Looper, No matter how many times you call Looper.prepare, it's a Looper object, a bit like a singleton pattern. Each thread can have more than one handler. When you create a hanlder, if you do not specify Looper, the looper of the current thread is associated with the handler constructor. So how did they relate to each other? The above code does not see their associated behavior. In fact, the mystery is in the handler constructor. public class Handler {
Final MessageQueue Mqueue; The associated MQ
Final Looper Mlooper; The associated Looper
Final Callback Mcallback;
Other properties
Public Handler () {
Here's a bunch of code to skip directly,,,
Gets the looper of the current thread with Mlooper
Mlooper = Looper.mylooper ();
Looper cannot be empty, if empty, that is, the thread does not have looper, direct error! That is, the default construction method can only be used in the Looper thread
if (Mlooper = = null) {
throw New RuntimeException (
"Can ' t create handler inside thread that have not called looper.prepare ()");
}
Important!!! The associated Looper MQ is directly used as its own MQ, so its messages are sent to the MQ of the associated Looper
Mqueue = Mlooper.mqueue;
Mcallback = null;
}
Other methods}
In fact, Looper inside the Loop () method is a dead loop, and constantly take messages from MQ, and then distributed to the corresponding handler processing.
Here, you should have a certain understanding of the message passing mechanism of this thread? It doesn't matter if you don't understand it, let's discuss it in the code parts official group.
See here, feel the thread and handler is already perfect, I want to download a thing, first create a thread, after downloading the UI update notification, one is very good. In fact, if you want to download 100 pictures, you can not open more than 100 threads to download it. This way the system resources are estimated to be unable to support, especially low-end mobile phones. Then what to do. The right approach is to use a thread pool, but it's very troublesome to maintain a thread pool yourself. Then Android has already helped us to think about this, we directly use the Asynctask class can! In fact, Asynctask internal has maintained a thread pool, interested students can read the source code. There is no more discussion here.
Finally, to comb the steps of the multithreading process;
If you are interested in this content, you can join Q group 187253654 discussion, follow the "Code parts" website or the public number
Android Threading message passing mechanism--looper,handler,message