标签:star get tac tar rup stack thread new ...
package cn.itcast.thread; /*守护线程(后台线程),一个进程中如果只剩下守护线程,那么守护线程也会死亡 * 定义守护线程的方式: * 调用线程的setDaemo(true)即可将线程设置为守护线程,其余用法与一般的线程一样*/ public class Demo8 extends Thread { public Demo8(String name) { super(name); } @Override public void run() { for(int i = 1; i <= 100; i++) { System.out.println("更新包下载中:" + i + "%" ); try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } if (i == 100) { System.out.println("更新包下载完成,准备安装..."); } } } public static void main(String[] args) { Demo8 d = new Demo8("后台线程"); //将线程设置为守护线程 d.setDaemon(true); System.out.println(d.isDaemon()); d.start(); for(int i = 1; i <= 100; i++) { System.out.println(Thread.currentThread().getName() + i); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } }
标签:star get tac tar rup stack thread new ...
原文地址:https://www.cnblogs.com/running-fish/p/9492723.html