• 创建Java守护线程


    Java中的守护程序线程是什么

    Java运行时利用特殊类型的线程来执行后台任务,这称为守护程序线程。这些支持线程管理诸如垃圾收集之类的后后任务。守护程序线程是特殊的,因为如果JVM中运行的唯一线程是守护程序线程,则Java运行时将关闭或退出。

    • 在Java中使用守护程序线程时,请特别小心地记住,运行时关闭时,守护程序线程的任务可能在执行过程中意外终止。

    在Java中将线程创建为守护进程就像调用setDaemon()方法一样简单。设置true意味着线程是一个守护进程;false表示不是。默认情况下,所有线程的初始值均为false,如下:

     /**
         * 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;
        }
    

    创建守护程序线程Demo

    主线程创建一个守护程序线程,该线程每1秒显示打印一条消息。然后,主线程休眠5秒钟。当守护程序线程仍在执行时,程序结束,因为当前唯一正在执行的线程是守护程序线程,所以守护进程也会结束。

    import org.slf4j.Logger
    
    class TSSS extends FanLibrary {
    
        public static Logger logger = getLogger(TSSS.class)
    
        public static void main(String[] args) {
            Thread daemonThread = new Thread(new Runnable() {
    
                public void run() {
                    // Repeat forever
                    while (true) {
                        logger.info("守护线程正在运行!" + TAB + Time.getDate());
                        sleep(1000)
                    }
                }
            });
            daemonThread.setDaemon(true);
            daemonThread.start();
            sleep(5000)
            logger.info("主线程结束!")
        }
    
    }
    

    控制台输出

    INFO-> 当前用户:fv,IP:192.168.0.102,工作目录:/Users/fv/Documents/workspace/fun/,系统编码格式:UTF-8,系统Mac OS X版本:10.15.4
    INFO-> 守护线程正在运行!	2020-05-24 22:02:36
    INFO-> 守护线程正在运行!	2020-05-24 22:02:37
    INFO-> 守护线程正在运行!	2020-05-24 22:02:38
    INFO-> 守护线程正在运行!	2020-05-24 22:02:39
    INFO-> 守护线程正在运行!	2020-05-24 22:02:40
    INFO-> 主线程结束!
    
    Process finished with exit code 0
    

    • 郑重声明:“FunTester”首发,欢迎关注交流,禁止第三方转载。更多原创文章:FunTester十八张原创专辑,合作请联系Fhaohaizi@163.com

    热文精选

  • 相关阅读:
    爬虫示例
    S20_DAY23--课堂笔记
    python--常用模块之正则
    S20_DAY22--课堂笔记
    win10系统重装
    CCF 命令行选项
    CCF 任务调度
    CCF 出现次数最多的数
    CCF ISBN
    CCF 最大的矩形
  • 原文地址:https://www.cnblogs.com/FunTester/p/12955432.html
Copyright © 2020-2023  润新知