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