Android uses handler to send messages between threads (between the main thread and the sub-thread) and (between the sub-thread and the sub-thread)
Keywords: Android uses handler to send messages between threads (between main thread and sub-thread), (between sub-thread and sub-thread)
I believe that we usually use Asynchronous threads to send messages to the main thread (UI thread. This article mainly studies Handler message sending. The main thread sends messages to the subthread, And the subthreads send messages to each other.
1. The main thread sends messages to the subthread.
The implementation process is relatively simple:
The main thread sends a message to the asynchronous thread. After receiving the message, the asynchronous thread sends another message to the main thread.
1. initialize the Handler of the main thread to receive messages from the subthread.
2. Start the asynchronous thread, create logoff in the asynchronous thread, and initialize the Handler of an asynchronous thread.
3. The main thread obtains the Handler of the asynchronous thread (this involves the knowledge of Inter-thread synchronization) and sends messages to the asynchronous thread.
4. After the asynchronous thread Handler receives the message, it obtains the Handler of the main thread and sends the message to the main thread.
5. The main thread receives a message from the asynchronous thread.
Note:
1. the main thread Handler or asynchronous thread Handler here refers to a Handler object bound to the corresponding thread message queue, input the logoff object of the corresponding thread in the Handler constructor.
2. the reason why thread locks are used for processing is that asynchronous thread startup is not real-time. When the main thread obtains the Handler of an asynchronous thread, it may be empty. If it is empty, wait for the asynchronous thread to initialize Handler first.
Code:
Package com. example. chen. myapplication; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. OS. logoff; import android. OS. message; import android. util. log;/*** Use Handler to test whether the main thread sends messages to the subthread.*@ AuthorChen*/Public class HandlerSimpleActivity extends Activity {private Handler mainHandler; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // start the asynchronous thread final AsyncThread asyncThread = new AsyncThread (); asyncThread. start (); // initialize the main thread's Handler mainHandler = new Handler (logoff. mylofter (), new Handler. callback () {@ Override public boolean handleMessage (Message MS) G) {// The main thread receives the message Log. e (HandlerSimpleActivity. class. getSimpleName (), MainHandler handler er Message curThread = + Thread. currentThread (). getName (); return false ;}}); // gets the Handler handler = asyncThread of the asynchronous thread. getHandler (); if (handler! = Null) {Log. e (HandlerSimpleActivity. class. getSimpleName (), MainThread Send Message curThread = + Thread. currentThread (). getName (); // send the handler message to the asynchronous thread. sendEmptyMessage (0 );}}/*** Asynchronous thread*/ Private class AsyncThread extends Thread {private Handler handler; @ Override public void run () {super. run (); Log. e (HandlerSimpleActivity. class. getSimpleName (), AsyncThread Start curThread = + Thread. currentThread (). getName (); logoff. prepare (); // initialize the message loop queue of asynchronous threads if (handler = null) {synchronized (AsyncThread. class) {// thread security // Add a Handler handler = new Handler (logoff. mylofter (), new Handler. callback () {@ Override public boolean handleMessage (Message msg) {// the asynchronous thread receives the Message Log. e (HandlerSimpleActivity. class. getSimpleName (), AsyncHandler extends er Message curThread = + Thread. currentThread (). getName (); // asynchronous thread sends messages to the main thread Log. e (HandlerSimpleActivity. class. getSimpleName (), AsyncThread Send Message curThread = + Thread. currentThread (). getName (); mainHandler. sendEmptyMessage (0); return false ;}}); // the asynchronous thread Handler initializes the Log. e (HandlerSimpleActivity. class. getSimpleName (), AsyncHandler Inited curThread = + Thread. currentThread (). getName (); // release the lock AsyncThread. class. policyall () ;}} logoff. loop ();}/*** Get the Handler of the asynchronous thread*@ Return */ Public Handler getHandler () {if (handler = null) {synchronized (AsyncThread. class) {// thread-to-thread security if (handler = null) {try {// the handler used to obtain the asynchronous thread is null. Release the lock and wait until the asynchronous thread Initialization is complete. Log. e (HandlerSimpleActivity. class. getSimpleName (), getHandler wait curThread = + Thread. currentThread (). getName (); AsyncThread. class. wait ();} catch (InterruptedException e) {e. printStackTrace ();} // asynchronous thread Handler Initialization is complete, and the main thread continues Log. e (HandlerSimpleActivity. class. getSimpleName (), getHandler notified curThread = + Thread. currentThread (). getName (); return handler;} else {return handler ;}}}}
Result:
2. subthreads use Handler to send messages to each other:
Wait for the next update (* ^__ ^ *) at 19:47:31, January 1, July 26, 2015 ......