Java中的守护程序线程是在后台连续执行的线程。您可以使用setDaemon()方法设置线程守护程序。
class adminThread extends Thread { adminThread() { setDaemon(true); } 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(true); thread.start(); } }
输出结果
thread = Thread[main, 5, main]