The android asynchronous message processing thread, which has been in an infinite loop each time it reads a message from the message queue, then calls back the method of processing messages in the handler handlermessage. If the message queue is empty, the thread hangs, waits for a message in the message queue to come in, and then wakes up the thread.
1.Android Asynchronous thread Internal structure
There is one or more handler objects inside the thread, and the external program sends asynchronous messages to the threads through the handler object, and the messages are passed handler to the message queue object, and each thread contains only one Message Queuing object. The thread main execution function reads the message from the message queue and callbacks the Handler object method Handlermessage ().
2.Thread Local Storage
The programmer creates a Message Queuing object for the thread through the static method of the Looper class prepare ().
Static final threadlocal<looper> sthreadlocal = new threadlocal<looper> ();
1 Private Static voidPrepareBooleanquitallowed) {2 if(Sthreadlocal.get ()! =NULL) {3 Throw NewRuntimeException ("Only one Looper could be created per thread");4 }5Sthreadlocal.set (NewLooper (quitallowed));6}
Thread-local object TLS, which is set in by the set method of Sthreadlocal.
3.Looper
Looper object: One is to create a message queue, and the other is to have the current thread provide a static method loop () into the loop and read the message from the message queue.
1 Private Looper (boolean quitallowed) {2 new MessageQueue (quitallowed); 3 true ; 4 Mthread = thread.currentthread (); 5 }
When you need to turn a thread into an asynchronous message processing thread, you should first call Looper.prepare () in the Run () function of the thread class to create a Message Queuing object for the thread. Then call Looper.loop () Causes the current thread to enter the message processing loop.
1 Public Static voidLoop () {2 FinalLooper me =Mylooper ();3 if(Me = =NULL) {4 Throw NewRuntimeException ("No Looper; Looper.prepare () wasn ' t called on the This thread. ");5 }6 FinalMessageQueue queue =Me.mqueue;7 8 //Make sure the identity of the the the the The local process,9 //And keep track of the What, identity token actually is.Ten binder.clearcallingidentity (); One Final LongIdent =binder.clearcallingidentity (); A - for (;;) { -Message msg = Queue.next ();//might block the if(msg = =NULL) { - //No message indicates that the message queue is quitting. - return; - } + - //This must is in a local variable with case a UI event sets the logger +Printer logging =me.mlogging; A if(Logging! =NULL) { atLogging.println (">>>>> dispatching to" + Msg.target + "" + -Msg.callback + ":" +msg.what); - } - - msg.target.dispatchMessage (msg); - in if(Logging! =NULL) { -Logging.println ("<<<<< finished to" + Msg.target + "" +msg.callback); to } + - //Make sure that during the course of dispatching the the //The identity of the thread wasn ' t corrupted. * Final LongNewident =binder.clearcallingidentity (); $ if(Ident! =newident) {Panax NotoginsengLOG.WTF (TAG, "Thread identity changed from 0x" -+ long.tohexstring (ident) + "to 0x" the+ long.tohexstring (newident) + "while dispatching to" ++ Msg.target.getClass (). GetName () + "" A+ Msg.callback + "what=" +msg.what); the } + - msg.recycle (); $}
4.Message Queue
As seen from the above, a message is read through Queue.next (), but there is no Message Queuing object in the framework, message Queue Two main functions read messages and add messages, respectively, next (), Enquencemessage (). But the real implementation is not in the framework layer but through JNI in C code.
1 Private native Static intnativeinit ();2 Private native Static voidNativedestroy (intptr);3 Private native Static voidNativepollonce (intPtrinttimeoutmillis);4 Private native Static voidNativewake (intPTR);
5.Handler
Before constructing the handler object, the Looper.prepare () must be executed, and in the Looper.loop () function, the different message corresponds to a different handler object, thus recalling the different handlermessage () functions.