setDaemon的简单使用

//copyright©liupengcheng
//http://www.cnblogs.com/liupengcheng

/**
* Created by Administrator on 2014/10/23.
*
*
setDaemon
public final void setDaemon(boolean on)将该线程标记为守护线程或用户线程。当正在运行的线程都是守护线程时,Java 虚拟机退出。
该方法必须在启动线程前调用。

该方法首先调用该线程的 checkAccess 方法,且不带任何参数。这可能抛出 SecurityException(在当前线程中)。

参数:
on - 如果为 true,则将该线程标记为守护线程。
*/

//copyright©liupengcheng
//http://www.cnblogs.com/liupengcheng

public class ThreadDemo3 {
    public static void main(String [] args){
        TestThread1 tt = new TestThread1();
        Thread t = new Thread(tt);
        t.setDaemon(true);
        t.start();
    }
}

//copyright©liupengcheng
//http://www.cnblogs.com/liupengcheng


class TestThread1 implements Runnable{
    public void run()
    {
        while (true)
        {
            System.out.println(Thread.currentThread().getName()+"is running");
        }
    }

}

//copyright©liupengcheng
//http://www.cnblogs.com/liupengcheng

//输出结果为空,因为所有线程都为守护线程,Java 虚拟机退出

原文地址:https://www.cnblogs.com/liupengcheng/p/4045886.html