Logoff. Loop ();
}
}
Package com. Simon;
ImportAndroid. App. activity;
ImportAndroid. OS. Bundle;
ImportAndroid. OS. message;
ImportAndroid. util. log;
ImportAndroid. OS. Handler;
Public Class Myhandler Extends Activity {
Static Final String tag = " Handler " ;
Handler H = New Handler (){
Public Void Handlemessage (Message MSG)
{
Switch (Msg. What)
{
Case Handler_test:
Log. D (tag, " The handler thread id = " + Thread. currentthread (). GETID () + " \ N " );
Break ;
}
}
};
Static Final Int Handler_test = 1 ;
/** Called when the activity is first created. */
@ Override
Public Void Oncreate (bundle savedinstancestate ){
Super . Oncreate (savedinstancestate );
Log. D (tag, " The main thread id = " + Thread. currentthread (). GETID () + " \ N " );
NewMythread (). Start ();
Setcontentview (R. layout. Main );
}
Class Mythread Extends Thread
{
Public Void Run ()
{
Message msg = New Message ();
MSG. What = Handler_test;
H. sendmessage (MSG );
Log. D (tag, " The worker thread id = " + Thread. currentthread (). GETID () + " \ N " );
}
}
}
InIn this example, we mainly print the thread of each module of this processing mechanism. The following is the running result of my machine:
23:40:51 09-10. 478: Debug/handler (302): The main thread id = 1 09-10 23:40:51. 569: Debug/handler (302): The worker thread id = 8 09-10 23:40:52. 128: Debug/handler (302): the handler thread id = 1
We can see that message processing is handled in the main thread. In the message processing function, you can safely call any resources in the main thread, including refreshing the interface. The worker thread and the main thread run in different threads. Therefore, pay attention to the competition between the two threads.
In the preceding example, you may notice that the main thread handler object is accessed in the working thread, and a message is added to the Message Queue to the handler object. Will there be inconsistency in the message queue column during this process? The answer is that the handler object will not be faulty, because the logoff object managed by the handler object is thread-safe. Whether it is to add messages to the Message Queue or read messages from the queue column, it is protected by synchronous objects, for more information, see logoff. java file. In the preceding example, the handler object is not modified, so the handler object cannot have data inconsistency.
Through the above analysis, we can draw the following conclusions:
1. If you use a worker thread to refresh the interface, Handler objects are recommended.
2. Pay attention to the competition between the working thread and the main thread. It is recommended that the handler object be constructed in the main thread (and do not modify it after the working thread is started; otherwise, data inconsistency will occur ), then, you can safely call interfaces such as sendmessage.
3. If the member variables of any main thread other than the hanlder object described in section 2 are called in the working thread, the thread synchronization issue should be carefully considered. If necessary, add a synchronization object to protect the variable.
4. The handlemessage interface of the handler object will be called in the main thread. In this function, you can call any variables and functions in the main thread with confidence to complete the UI update task.
5. Many Android APIS also use the handler thread feature as a callback function variant to notify callers. In this way, the android framework can send messages to the caller's thread message queue in its thread without worrying about thread synchronization.
An in-depth understanding of the android message processing mechanism is very important for application development. It also gives you a deeper understanding of thread synchronization. The above is a summary of Simon's recent study of the android message processing mechanism. If there are any errors, please do not hesitate to advise.