-
当主线程挂掉后,守护线程会跟随挂掉的
public class ThreadDemo { public static void main(String[] args) throws Exception { System.out.println("主线程开始执行!!!"); System.out.println("主线程准备启动一个子线程........"); Thread thread = new Thread(new Runnable() { @Override public void run() { System.out.println("子线程开始执行啦,,,,,"); while(true) { try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("子线程打印# # # # # "); } } }); //使用下面这个,子线程就会编程守护线程 thread.setDaemon(true); thread.start(); System.out.println("主线程启动子线程后的语句"); thread.sleep(10000); } }
06主线程和守护线程
最新推荐文章于 2022-11-15 17:31:57 发布