1.守护线程的设置setdaemon(true)必须先放在start()之前,否则程序会出错。
2.守护线程中创建的所有子线程都是守护线程。
使用jojn()方法,无论线程是用户线程还是守护线程,都会等待一个线程完成。
实例
public static void main(string[] args) throws interruptedexception { thread thread = new thread(new runnable() { @override public void run() { for (int i = 1; i <= 10; i++) { // 打印 i 信息 system.out.println(i: + i + ,isdaemon: + thread.currentthread().isdaemon()); try { // 休眠 100 毫秒 thread.sleep(100); } catch (interruptedexception e) { e.printstacktrace(); } } } }); // 启动线程 thread.start(); // 设置为守护线程 thread.setdaemon(true);}
以上就是java守护线程的注意事项有哪些?的详细内容。
