启动线程的注意事项
无论何种方式,启动一个线程,就要给它一个名字!这对排错诊断
系统监控有帮助。否则诊断问题时,无法直观知道某个线程的用途。
Thread thread = new Thread("thread name") {
public void run() {
// do xxx
}
};
thread.start();
Thread thread = new Thread() {
public void run() {
// do xxx
}
};
thread.setName("thread name");
thread.start();
public class MyThread extends Thread {
public MyThread() {
super("thread name");
}
public void run() {
// do xxx
}
}
MyThread thread = new MyThread ();
thread.start();
Thread thread = new Thread(task); // 传入任务
thread.setName(“thread name");
thread.start();
Thread thread = new Thread(task, “thread name");
thread.start();
1
3
2
4
5