1.其实可以理解Handler为主线程和另外的线程之间进行数据更新的东东,并且Handler在主线程中,并在Handler直接调用线程的run方法
package com.Handler02;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
public class Handler02Activity extends Activity {
/** Called when the activity is first created. */
private Handler handler=new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handler.post(thread1);
setContentView(R.layout.main);
System.out.println("================Main==============="+Thread.currentThread().getId());
System.out.println("============Main========="+Thread.currentThread().getName());
}
Runnable thread1=new Runnable() {
@Override
public void run() {
System.out.println("======thread1==============="+Thread.currentThread().getId());
System.out.println("========thread1============"+Thread.currentThread().getName());
try {
Thread.sleep(10000);
} catch (Exception e) {
// TODO: handle exception
}
}
};
}
结果是主线程等待10s后才显示出来,并且线程ID