This article uses examples to verify and explain whether the thread enabled with the handler object is in the same thread as the main thread.
The program structure is as follows:
[1] The source code in handleractivity. Java is as follows:
[HTML]View plaincopyprint?
- Package com. andyidea. handlerdemo2;
- Import Android. App. activity;
- Import Android. OS. Bundle;
- Import Android. OS. Handler;
- Import Android. OS. handlerthread;
- Import Android. OS. logoff;
- Import Android. OS. message;
- Import Android. util. log;
- Public class handleractivity extends activity {
- Private handler = new handler ();
- @ Override
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- // Handler. Post (R );
- Setcontentview (R. layout. Main );
- Log. E ("activity -->", thread. currentthread (). GETID () + "");
- Log. E ("activityname -->", thread. currentthread (). getname () + "");
- // Same thread as the main thread
- Handler. Post (R );
- // Different from the main thread
- Handlerthread = new handlerthread ("handler_thread ");
- Handlerthread. Start ();
- // After the handlerthread thread object is started,
- // You can use the handlerthread. getlooper () method to obtain the looper object.
- Myhandler = new myhandler (handlerthread. getlooper ());
- Message MSG = myhandler. obtainmessage ();
- MSG. sendtotarget (); // message sending Method
- }
- // Create a thread object
- Runnable r = new runnable (){
- @ Override
- Public void run (){
- Log. E ("Handler -->", thread. currentthread (). GETID () + "");
- Log. E ("handlername -->", thread. currentthread (). getname () + "");
- Try {
- Thread. Sleep (10*1000 );
- } Catch (interruptedexception e ){
- E. printstacktrace ();
- }
- }
- };
- // Create a handler object
- Class myhandler extends handler {
- Public myhandler (){
- }
- Public myhandler (low.logoff ){
- Super (logoff );
- }
- @ Override
- Public void handlemessage (Message MSG ){
- Log. E ("Handler -->", thread. currentthread (). GETID () + "");
- Log. E ("Handler -->", thread. currentthread (). getname () + "");
- }
- }
- }
Package COM. andyidea. handlerdemo2; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. OS. handler; <br/> Import android. OS. handlerthread; <br/> Import android. OS. logoff; <br/> Import android. OS. message; <br/> Import android. util. log; </P> <p> public class handleractivity extends activity {</P> <p> private handler = new handler (); </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> // handler. post (r); <br/> setcontentview (R. layout. main); <br/> log. E ("activity -->", thread. currentthread (). GETID () + ""); <br/> log. E ("activityname -->", thread. currentthread (). getname () + ""); </P> <p> // same thread as the main thread <br/> handler. post (r); </P> <p> // different from the main thread <br/> handlerthread = new handlerthread ("handler_thread"); <br/> handlerthread. start (); <br/> // After the handlerthread thread object is to be started, <br/> // use handlerthread. the getlooper () method can obtain the looper object <br/> myhandler = new myhandler (handlerthread. getlooper (); <br/> message MSG = myhandler. obtainmessage (); <br/> MSG. sendtotarget (); // message sending method <br/>}</P> <p> // create a thread object <br/> runnable r = new runnable () {</P> <p> @ override <br/> Public void run () {<br/> log. E ("Handler -->", thread. currentthread (). GETID () + ""); <br/> log. E ("handlername -->", thread. currentthread (). getname () + ""); <br/> try {<br/> thread. sleep (10*1000); <br/>} catch (interruptedexception e) {<br/> E. printstacktrace (); <br/>}< br/> }; </P> <p> // create a handler object <br/> class myhandler extends handler {</P> <p> Public myhandler () {</P> <p >}</P> <p> Public myhandler (loggle) {<br/> super (loggle ); <br/>}</P> <p> @ override <br/> Public void handlemessage (Message MSG) {<br/> log. E ("Handler -->", thread. currentthread (). GETID () + ""); <br/> log. E ("Handler -->", thread. currentthread (). getname () + ""); <br/>}</P> <p >}< br/>}
After running the program, we can see the following results through the log. e output in the logcat console:
Through verification, we can see that if we only put a subthread object into the main thread queue through the post () method of the handler object, it is not a subthread is not enabled. The handlerthread object method enables a new thread to execute the program.