关于Thread 几个写法的试验与疑问
package com.example.mydemo01; import java.util.Timer; import java.util.TimerTask; import android.os.Bundle; import android.os.Handler; import android.app.Activity; import android.util.Log; import android.view.Menu; import android.widget.TextView; public class MyMain extends Activity { private TextView tv1; private int count; private final Handler mHandler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case 1: tv1.setText("count:" + count); count++; break; default: break; } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_main); tv1 = (TextView) findViewById(R.id.textView1); } @Override protected void onResume() { count = 0; // myPost();//error // new MyThread().start();//ok // myNewTNewR();//error // new MyRunnable().run();//error // myTimer();//ok // myDirectFor();//error /** ERROR * Thread MyBitThread = new Thread(){ public void run() { for (int i = 0; i < 99999; i++) { Log.d("linsen", "i=" + i); mHandler.sendEmptyMessage(1); } }; }; MyBitThread.start();*/ new MyBigThread().start(); super.onResume(); } class MyBigThread extends Thread{ @Override public void run() { for (int i = 0; i < 99999; i++) { Log.d("linsen", "i=" + i); mHandler.sendEmptyMessage(1); } super.run(); } } private void myDirectFor() { for (int i = 0; i < 99999; i++) { Log.d("linsen", "i=" + i); mHandler.sendEmptyMessage(1); // runOnUiThread(new Runnable() { // // @Override // public void run() { // // tv1.setText("count:" + count); // // count++;//error // // mHandler.sendEmptyMessage(1); // // } // }); } } private void myTimer() { new Timer().schedule(new TimerTask() { @Override public void run() { for (int i = 0; i < 50; i++) { Log.d("linsen", "i=" + i); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } mHandler.sendEmptyMessage(1); } } }, 500, 500); } class MyThread extends Thread { @Override public void run() { for (int i = 0; i < 50; i++) { Log.d("linsen", "i=" + i); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } mHandler.sendEmptyMessage(1); } super.run(); } } class MyRunnable implements Runnable { @Override public void run() { for (int i = 0; i < 50; i++) { Log.d("linsen", "i=" + i); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } mHandler.sendEmptyMessage(1); } } } private void myNewTNewR() { Thread t = new Thread(new Runnable() { public void run() { for (int i = 0; i < 20; i++) { Log.d("linsen", "i=" + count); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } // tv1.setText("count:" + count); // count++; mHandler.sendEmptyMessage(1); } } }); t.start(); } private void myPost() { mHandler.post(new Runnable() { @Override public void run() { for (int i = 0; i < 20; i++) { Log.d("linsen", "i=" + count); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } tv1.setText("count:" + count); count++; } } }); } private void myRunOnUiThread() { runOnUiThread(new Runnable() { @Override public void run() { // try { // this.wait(1000); // } catch (InterruptedException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } for (int i = 0; i < 10; i++) { tv1.setText("count:" + count); count++; } } }); } }