我正在从“Think in Java”中学习守护线程内容,但是,当我尝试运行守护线程并期待一些输出时,它没有给我任何结果......我的编程 IDE 是 Eclipse,操作系统是 ubuntu13。 04...请告诉我发生了什么事。如果注释掉“t.setDaemon(true);”,以下代码将输出 msg
public class Practice implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(1000);
System.out.println(Thread.currentThread()+" "+this);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Daemon is working");
}
public static void main(String[] args){
Thread t = new Thread(new Practice());
t.setDaemon(true);
t.start();
}
}
最佳答案
它根本没有时间打印消息,因为 main 退出得太快,尝试暂停一秒钟,你就会看到它
Thread t = new Thread(new Practice());
t.setDaemon(true);
t.start();
Thread.sleep(1000); <--
关于java - 简单的守护线程不输出消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21082074/