zoukankan      html  css  js  c++  java
  • 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;
        }
  • 相关阅读:
    Liferay7 BPM门户开发之1:Liferay7开发环境准备
    Liferay-Activiti 企业特性功能介绍 (新版Liferay7)
    Liferay-Activiti 功能介绍 (新版Liferay7基本特性)
    Java入门开发POI读取导入Excel文件
    JAVA动态代理和方法拦截(使用CGLib实现AOP、方法拦截、委托)
    JFrame、JPanel 、Layout开发的简单例子
    Smart/400开发上手5: Cobol开发标准
    Smart/400开发上手4: 调试Cobol代码 (DEBUG with QBATCH)
    Netbeans Platform 工程,免安装JDK
    网络延迟测试结果
  • 原文地址:https://www.cnblogs.com/qitian1/p/6461529.html
Copyright © 2011-2022 走看看