【Java】【多线程】守护线程

6 篇文章 0 订阅

【Java】【多线程】守护线程


学而不思则罔,思而不学则殆

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

The Java Virtual Machine exits when the only threads running are all daemon threads.

这句话是官方的描述,Java虚拟机退出,当运行的线程全是守护线程的时候。
必须在线程start之前调用

守护线程是比较特殊的线程。

非守护线程

    private static void testThree() {
        Thread thread = new Thread(new Runnable() {
            public void run() {
                int i = 0;
                while (true) {
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("守护线程工作中..." + i++);
                    // TODO: 2020/8/31 做一些守护线程的工作
                }
            }
        });
        //thread.setDaemon(true); //将线程设置为说话线程
        thread.start();  //启动线程

        try {
            System.out.println("主线程休眠5s");
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("主线程结束");
    }

创建一个永远不结束的非守护线程,该程序永远不会退出(正常情况下)。

在这里插入图片描述
5s过后,main线程结束,但是子线程还在一直运行,虚拟机没有退出,红点一直显示中。此时虚拟机中还存在工作的非守护线程,不会退出。

守护线程

    private static void testThree() {
        Thread thread = new Thread(new Runnable() {
            public void run() {
                int i = 0;
                while (true) {
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("守护线程工作中..." + i++);
                    // TODO: 2020/8/31 做一些守护线程的工作
                }
            }
        });
        thread.setDaemon(true); //将线程设置为说话线程
        thread.start();  //启动线程

        try {
            System.out.println("主线程休眠5s");
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("主线程结束");
    }

添加一个守护线程
在这里插入图片描述
main线程结束后,守护线程也退出了,红点没有,且code = 0,正常提出。所以当只有守护线程的时候,虚拟机会正常退出。

作用

守护线程经常用作执行后台任务(后台线程),当你希望关闭某些线程的时候或者退出JVM的时候,一些线程能够自动关闭,这个时候就可以考虑使用守护线程。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值