Java线程和多线程(六)——守护线程

当我们在Java中创建线程的时候,这个线程在默认的情况下是一个用户线程,并且,如果这个线程在运行,那么JVM就不会终结这个应用。和用户线程不同,当一个线程被标记为守护线程的时候,JVM在用户线程结束的时候,是不会持续等待守护线程结束的,而是直接结束程序,并且结束程序中相关的守护线程。

Thread.setDaemon(true)方法可以用来将线程置为守护线程,参考下面的例子:

package com.sapphire.threads;

public class JavaDaemonThread {

    public static void main(String[] args) throws InterruptedException {
        Thread dt = new Thread(new DaemonThread(), "dt");
        dt.setDaemon(true);
        dt.start();
        //continue program
        Thread.sleep(30000);
        System.out.println("Finishing program");
    }

}

class DaemonThread implements Runnable{

    @Override
    public void run() {
        while(true){
            processSomething();
        }
    }

    private void processSomething() {
        try {
            System.out.println("Processing daemon thread");
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

当执行上面的程序时,JVM会包含main方法这个线程,而后在main方法中创建一个用户线程,然后是变成了守护线程。当main函数结束的时候,程序就会结束,而且JVM也会同时关闭掉守护线程。

程序的输出如下:

Processing daemon thread
Processing daemon thread
Processing daemon thread
Processing daemon thread
Processing daemon thread
Processing daemon thread
Finishing program

但是,如果我们没有将线程置为守护线程的话,程序就永远都不会结束,就算main函数的线程执行完毕,程序仍然会继续下去。

通常来说,我们创建一个守护线程为了一些跟系统不那么相关的功能。举例来说,日志线程或者监控系统资源状态的线程等。

守护线程的配置必须在线程启动之前,参考Javadoc

    /**
     * Marks this thread as either a {@linkplain #isDaemon daemon} thread
     * or a user thread. The Java Virtual Machine exits when the only
     * threads running are all daemon threads.
     *
     * <p> This method must be invoked before the thread is started.
     *
     * @param  on
     *         if {@code true}, marks this thread as a daemon thread
     *
     * @throws  IllegalThreadStateException
     *          if this thread is {@linkplain #isAlive alive}
     *
     * @throws  SecurityException
     *          if {@link #checkAccess} determines that the current
     *          thread cannot modify this thread
     */
    public final void setDaemon(boolean on) {
        checkAccess();
        if (isAlive()) {
            throw new IllegalThreadStateException();
        }
        daemon = on;
    }
原文地址:https://www.cnblogs.com/qitian1/p/6461529.html