用什么方法在Java中创建守护程序线程?
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
热门推荐
10 毕业祝福语保安文案简短
11 毕业英语祝福语大全简短
12 高考毕业祝福语简短励志
13 成年女性生日祝福语简短
14 打游戏通关祝福语简短
15 朋友搬迁新房祝福语简短
16 给舍友送祝福语简短
17 猪年幽默祝福语简短
18 孩子转学离群祝福语简短