Daemon Thread 怎么理解?Daemon Thread 名为 后台线程。
如下代码,我的想法是当主线程(main)结束掉后,应该并不影响后台线程(生成文件)
但是 结果是:当main结束掉后, 其他的Daemon 线程并没有生成文件,也就是说 并没有运行。
希望大家 指点指点!public class DaemonThreadTest extends Thread{
private int no;
public DaemonThreadTest(int no) {
this.no = no;
}
@Override
public void run() {
try {
Thread.sleep(2000);
File f = new File("H:\\decisci\\temp\\f"+no+".txt");
if(! f.exists()){
f.createNewFile();
}
System.out.println("thread ended");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
for(int i=0; i<5; i++){
Thread thread = new DaemonThreadTest(i);
thread.setDaemon(true);
thread.start();
System.out.println("thread "+i+" start");
}
}}