How Android listens for the start and end of a thread

Source: Internet
Author: User

Method One: Rotation

For example, the main thread to wait for the child thread to get the variable "val" value to start with "Val" value to work, using rotation method is as follows:

 Public classSubthread extends thread{PrivateBoolean flag =false;//flag bit that identifies whether the main thread or other object should be notified of the change to do things@Override Public voidrun () {super.run ();  for(intI=0;i<5; i++) {System. out. println (Thread.CurrentThread (). GetName () +" Do"+ i +"thing"); } Flag=true;  for(intI=5;i<Ten; i++) {System. out. println (Thread.CurrentThread (). GetName () +" Do"+ i +"thing"); }        }         PublicBoolean Getflag () {returnFlag; }} Public classTest { Public Static voidMain (string[] args) {subthread subthread=NewSubthread ();                Subthread.start ();  while(true){                        if(Subthread.getflag ()) {System. out. println (Thread.CurrentThread (). GetName () +"Do something ...");                                DoWork ();  Break; }                 }        }        Private Static voiddoWork () {System. out. println (Thread.CurrentThread (). GetName () +"Do work ..."); }}

It can be seen that the effect is actually achieved, but this method is too low, the disadvantage is as follows:
(1) The main thread loop judgment flag bit is wasted CPU performance
(2) If there are other threads that need to be judged, it is also necessary to write a dead loop in the thread, the code is too free of design ideas
, see here maybe someone has thought of another way--callback!

Method Two, callback
A callback is a method of invoking another object that passes "yourself", and then another object calls "its own method" at some point, and the code is as follows

 Public InterfaceThreadcallback {voidThreadstartlisener (); voidThreadendlisener ();} Public classSubrunnable implements Runnable {PrivateThreadcallback Mthreadcallback;  Publicsubrunnable (Threadcallback threadcallback) { This. Mthreadcallback =Threadcallback; } @Override Public voidrun () {Mthreadcallback.threadstartlisener ();  for(intI=0;i<5; i++) {System. out. println (Thread.CurrentThread (). GetName () +"Do something"+i);        } mthreadcallback.threadendlisener (); }} Public classThreadcallbacktest implements threadcallback{Private StaticThreadcallbacktest mthreadcallbacktest =Newthreadcallbacktest ();  Public Static voidMain (string[] args) {System. out. println (Thread.CurrentThread (). GetName () +"Start"); NewThread (Newsubrunnable (mthreadcallbacktest)). Start (); } @Override Public voidThreadstartlisener () {System. out. println (Thread.CurrentThread (). GetName () +"thread, knowing that the subrunnable thread is starting to perform the task"); } @Override Public voidThreadendlisener () {System. out. println (Thread.CurrentThread (). GetName () +"thread, knowing that the subrunnable thread task execution is over"); }}

The output results are as follows:

Main starts Thread-0 thread and knows subrunnable thread is starting to perform Thread-0 do something 0thread-0 does something 1thread-0 do something 2thread-0 do Something 3thread-0 do something 4thread-0 thread, know subrunnable thread task execution ended Discover Threadstartlisener () method and Threadendlisener () Methods are executed in a child thread. This means that although both callback methods are executed successfully, the thread used to execute is incorrect.

How Android listens for the start and end of a thread

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.