Thread类的setDaemon()方法用于将特定线程标记/设置为守护线程或用户线程。当所有正在运行的线程都是守护程序线程时,Java虚拟机将退出。必须在线程启动之前调用此方法。
import java.lang.*;
class adminThread extends Thread {
adminThread() {
setDaemon(false);
}
public void run() {
boolean d = isDaemon();
System.out.println("daemon = " + d);
}
}
public class ThreadDemo {
public static void main(String[] args) throws Exception {
Thread thread = new adminThread();
System.out.println("thread = " + thread.currentThread());
thread.setDaemon(false);
thread.start();
}
}输出结果
thread = Thread[main,5,main] daemon = false