【发布时间】:2011-12-30 09:19:47
【问题描述】:
大家好,当封闭它的线程完成时,守护线程会停止工作吗?或者守护线程会在“主”线程完成时停止?
我在 jre6 上测试了这个例子,结果是daemon thread stopped working when the enclosing it thread finished。请注意,java 文档说,当没有其他应用程序线程存在时,守护程序线程将被终止。并且没有说当父非守护线程保留时守护线程被杀死。
请给我答案。请向我发送有关此问题的任何材料。 对不起我的英语。
public class Main {
public static void main(String[] args) {
Thread simple = new Thread(new SimpleTask());
simple.start();
}
}
class SimpleTask implements Runnable {
public void run() {
try {
Thread daemon = new Thread(new DaemonTask());
daemon.setDaemon(true);
daemon.start();
Thread.sleep(5000);
} catch (InterruptedException e) {}
};
}
class DaemonTask implements Runnable {
public void run() {
int i = 0;
while (true) {
try {
System.out.println("a" + (i++));
Thread.sleep(500);
} catch (InterruptedException e) {}
}
}
}
【问题讨论】:
标签: java multithreading daemon