1. Inter-thread communication mechanism Firstly, looper, handler and MessageQueue all realize the inter-thread communication mechanism in Android system. If you need to pass a message between A and B two sub-threads, first bind each child thread to a set of handler, Looper, MessageQueue mechanisms, and then these three objects correspond to the thread to which they belong. The A thread then sends a message by calling the B-thread's handler object. This message is sent to the MessageQueue of the B thread by handler, and the Looper object belonging to the B thread has been iterating through the MessageQueue in the for loop indefinitely, and once a new message is found in the message queue, the message is processed. The Heandlemessage method that will callback itself handler during processing. Thus the communication between different threads is realized. 2, Looper implementation principle the Looper class contains a Message Queuing object and a thread object. When Looper is created, a message queue is automatically created and the internal thread object is pointed to the thread that created the Looper. When the Looper is turned on (Looper.loop ()), it automatically enters the infinite for loop, continually traverses the message queue, and if no message is blocked, a message callback handler's Handlemessage method for processing. 3, Looper.prepare () first, to use the Looper mechanism typically creates a handler object in the current thread, which automatically creates a Looper object and message queue, which is a message queue that belongs to the current thread space. At this point, however, the looper does not traverse or bind to the current thread. Within the Looper object, an empty Message Queuing object and a null thread are also included. With the Looper.prepare () method, the message queue is first pointed to the message queue of the current thread, and the empty thread also points to the current thread. Thus the binding is implemented.
The implementation principle of Looper in Android, why Call Looper.prepare () is associated with a Looper object in the current thread, how it is implemented.