The main thread in Android

Source: Internet
Author: User

The main thread in Android does not need to be created.

The Thread.Sleep Setup program is blocked.

1  Public classMainactivity extends Appcompatactivity {2 3 @Override4     protected voidonCreate (Bundle savedinstancestate) {5 super.oncreate (savedinstancestate);6 Setcontentview (r.layout.activity_main);7Findviewbyid (R.ID.BT). Setonclicklistener (NewView.onclicklistener () {8 @Override9              Public voidOnClick (View v) {Ten                 Try { OneThread.Sleep ( +); ASystem. out. Print ("The thread is running, is sleeping"); -}Catch(interruptedexception e) { - e.printstacktrace (); the                 } -             } -         }); -     } +}

When you create a new child thread, pressing the button will not kill it and will pop up immediately.

1 NewThread () {2 @Override3                      Public voidrun () {4                         Try {5                              while(true){6Sleep +);7System. out. Print ("Hello, this is the new thread!");8                             }9}Catch(interruptedexception e) {Ten e.printstacktrace (); One                         } A                     } -}.start ();

The main thread in Android

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.