java 多线程-守护线程

守护线程daemon,是为用户线程服务的,在start前设置
默认情况下我们的线程是用户线程
线程分为用户线程和守护线程
虚拟机必须确保用户线程执行完毕
虚拟机不用等待守护线程执行完毕
如后台记录操作日志、监控内存使用等
Thread对象.setDaemon(true);默认为false

public class n {

public static void main(String[]args) throws InterruptedException
{
    test t=new test();
    test2 t2=new test2();

    new Thread(t).start();

    Thread tt2=new Thread(t2);
    tt2.setDaemon(true);//在t用户线程结束后结束
    tt2.start();

}
}
class test implements Runnable
{
public void run()
{
    for(int i=1;i<=365*100;i++)
    {
        System.out.println("me");
    }
    System.out.println("he");
}
}
class test2 implements Runnable{
public void run()
{
    for(;true;)
    {
        System.out.println("she");
    }
}
}

猜你喜欢

转载自blog.51cto.com/14437184/2428993