Android Sub-Threading test

Source: Internet
Author: User

    Private volatile Boolean mstopped = false;    private int i;    TextView TV1;    TextView TV2;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main3);        TV1 = (TextView) Findviewbyid (R.ID.TV1);        TV2 = (TextView) Findviewbyid (R.ID.TV2);    Button Button4 = (button) Findviewbyid (R.ID.BUTTON4);    Gets the start button button5 = (button) Findviewbyid (R.ID.BUTTON5); Get the Stop button//Start button Button4.setonclicklistener (new View.onclicklistener () {@Override PU                    Blic void OnClick (View v) {if (mstopped = True) {mstopped = false;                    LOG.I ("Main thread id", string.valueof (Thread.CurrentThread (). GetId ()));                    LOG.I ("Main thread var", string.valueof (Thread.CurrentThread (). GetName ()));                    Fgetsum (string.valueof ("MYTV1"), 1); Fgetsum (string.valueof ("MYTV2"), 2);               }            }        }); End Thread button Button5.setonclicklistener (new View.onclicklistener () {@Override public void onclic            K (View v) {mstopped = true;    }        }); }//defines the Handler message processing class, Handler allows the sending and processing of a message or rannable to receive a message notification in the MessageQueue of the thread to which it resides, and refreshes//handler//generally in this            Refresh UI Final Handler handler2 = new Handler () {@Override public void Handlemessage (Message msg) {                    Switch (msg.what) {case 0x101:tv1.settext (string.valueof (msg.obj.toString ()));                Break                    Case 0x102:tv2.settext (string.valueof (msg.obj.toString ()));                Break                    Default:tv1.setText (String.valueof ("Unknown Msg.what"));            Break    }        }    }; Define Runnable interface//Send message in child thread to handler//runnable does not run in UI, so there is no direct modification to UI object Properties private void FgetsUm (final String strname, final int tvid) {thread thread = new Thread () {@Override public vo                    ID run () {while (mstopped = = false) {i++;                        try {thread.sleep (1000);                        LOG.I ("Child thread ID" + strname, String.valueof (Thread.CurrentThread (). GetId ()));                        LOG.I ("Child thread name" + strname, String.valueof (Thread.CurrentThread (). GetName ()));                        Message message = Handler2.obtainmessage ();                       MESSAGE.ARG1 = Tvid; If tvid=1 incoming time, pass in I if (tvid==1) {SimpleDateFormat SDF = new Simpledatef                            Ormat ("Yyyy-mm-dd hh-mm-ss", Locale.getdefault ());                            message.what=0x101;                        Message.obj = string.valueof (Sdf.format (New Date ()));                           }else{message.what=0x102; Message.obj =i;                    } handler2.sendmessage (message);                    } catch (Interruptedexception e) {e.printstacktrace ();        }                }        }        };        Thread.Start ();    thread = NULL; }

Android and WinForm, the main thread can not directly update the control on the UI, otherwise the interface will be suspended animation (such as progress bar, download and other tasks)

There are backgroundworker under ASP, and Android is generally implemented with handler+thread+message, for people who used to. NET say, look at the Java program will be very unaccustomed, many of the functions are more troublesome than. Net.

Also check the Java data, if you want to create n threads, it is recommended to use a thread program pool (executorservice) better.

Android Sub-Threading test

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.