multithreading is convenient, but improper use may cause many problems, for example, the following example:
Package COM. example. anrtest; import android. app. activity; import android. OS. bundle; import android. util. log; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. toast; public class mainactivity extends activity {private byte [] Lock = new byte [1]; @ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); showth (); button BTN = (button) findviewbyid (R. id. BTN); BTN. setonclicklistener (New onclicklistener () {@ override public void onclick (view v) {showtoast () ;}});} private void showth () {New thread (New runnable () {@ override public void run () {synchronized (LOCK) {While (true) {log. E ("H3C", "Show:" + thread. currentthread (). GETID (); try {thread. sleep (10000);} catch (interruptedexception e) {e. printstacktrace ();}}}}}). start ();} private void showtoast () {synchronized (LOCK) {toast. maketext (this, "click", 100 ). show ();}}}
In order to wait for the thread execution result, a synchronization lock is added to the UI thread when the button is clicked, which means no thread is used.
Therefore, you must note that do not add synchronization locks in the UI thread! Otherwise it is easy to ANR!