package 后台程序;
class DamonThread implements Runnable{ //实现Runnab接口
public void run(){
while(true){
System.out.println(Thread.currentThread().getName()+"--is running.");
}
}
}
public class 后台程序 {
public static void main(String[] args) {
System.out.println("main线程是后台程序吗?"+Thread.currentThread().isDaemon());//判断main方法是后台程序吗
DamonThread dt = new DamonThread(); //创建一个damonThread对象dt
Thread t = new Thread(dt,"后台程序"); //创造一个线程对象t共享dt资源
System.out.println("t线程是默认程序吗?"+t.isDaemon()); //判断t是后台程序艾玛
t.setDaemon(true); //将t设置成后台程序
t.start();
for(int i = 0; i <100; i++)
System.out.println(i);
}
}
//只要有一个前台程序还在运行,程序就不会结束,换而言之,只要没有了前台程序,程序就终止了
//新创建的线默认是前台程序,可以通过setDaemon方法将其设置成后台程序