用什么方法在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 婆婆66岁祝福语简短
13 今天孩子高考祝福语简短
14 送彩票生日祝福语简短
15 分别祝福语简短情话英文
16 画室揭牌仪式祝福语简短
17 女友妈妈生日祝福语简短
18 离别赠言离职祝福语简短