用户线程与守护线程
thread.setDaemon(true)public class DaemonThreadExample {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("daemon finally run ");
}
});
thread.setDaemon(true);
thread.start();
System.out.println("main thread end, jvm will close...");
}
}
//输出如下
main thread end, jvm will close...参考
Last updated