Package Com.jrhcode.morethreadtest;import Java.util.concurrent.executors;import Android.os.asynctask;import Android.os.bundle;import Android.os.handler;import Android.app.activity;import Android.view.Menu;import Android.widget.textview;import android.widget.toast;/** * * Main function is: in the child thread to change the display of TextView * * Android multithreaded programming 1: Through Han Dler Update UI Component Handler.post (new Runnable ()) in the newly-started thread; 2:handler.postdelayed (new Runnable, delay); 3:activity.runonuithread (); 4:asynctask 5: Thread pool * * @author Administrator * */public class Mainactivity extends Activity {TextView tv;private Handl Er mhandler;private String TAG = MainActivity.class.getSimpleName (); int i = 0; @Overrideprotected void onCreate (Bundle sav Edinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); TV = (TextView) Findviewbyid (r.id.tv); mhandler = new Handler ();//Inithandler ();//InitHandler2 ();//InitHandler3 ();//UpdateTV ();// UPDATETVV (); Threedpool ();} private void Threedpool () {//TODO AuTo-generated method Stubexecutors.newsinglethreadexecutor (). Submit (New Runnable () {@Overridepublic void Run () {//TODO Auto-generated Method StubSystem.out.println (Thread.CurrentThread () getName ()); Tv.settext (TAG); Mhandler.post (new Runnable () {@Overridepublic void Run () {//TODO auto-generated method Stubtv.settext ("This is Handler post"),}});});} /** * */private void Updatetvv () {//TODO auto-generated method Stubnew MyTask (). Execute (); /** * Method 3 */private void UpdateTV () {//TODO auto-generated method stubnew Thread (new Runnable () {@Overridepublic void run () {//Todo auto-generated method StubMainActivity.this.runOnUiThread (new Runnable () {@Overridepublic void Run () {//TODO Auto-generated method Stubtv.settext (TAG);}}). Start ();} /** * Delay 3 sec */private void InitHandler3 () {//TODO auto-generated method stubnew Thread (new Runnable () {@Overridepublic voi D run () {//Todo auto-generated method stubmhandler.postdelayed (new Runnable () {@Overridepublic void Run () {//Todo auto-g enerated mEthod Stubtv.settext (TAG);}}, 3000);}). Start ();} /** * in child thread method */private void InitHandler2 () {//TODO auto-generated method stubnew Thread (new Runnable () {@Overridepublic void Run () {//TODO auto-generated method Stubmhandler.post (new Runnable () {@Overridepublic void run () {System.out.printl N (Thread.CurrentThread (). GetName ()); Tv.settext (TAG);}}). Start ();} /** * Mothod 1; The method is to manipulate the UI in the note thread; */private void Inithandler () {//TODO auto-generated method Stubmhandler.post (new Runnable () {@Overridepublic void run () { Toast.maketext (Mainactivity.this,thread.currentthread (). GetName (), 0). Show (); Tv.settext (TAG);});} Class MyTask extends Asynctask<string, Void, string> {@Overrideprotected string Doinbackground (String ... params) { Todo auto-generated method stubtry {thread.sleep;} catch (Interruptedexception e) {//TODO auto-generated catch B Locke.printstacktrace ();} return null;} @Overrideprotected void OnPostExecute (String result) {//TODO auto-generated method Stubsuper.onpostexecute (result); Tv.settext (TAG);}}}
Android sub-thread Update UI Interface Summary