Java多线程系列2(守护线程)

12 篇文章 0 订阅

1 用户线程和守护线程 
Java虚拟机中,有两种线程:用户线程(User Thread)和守护线程(Daemon Thread)。 
平时我们编写代码中的main函数所在的线程一般就是用户线程。 
守护线程是相对于用户线程而言的。守护线程的优先级低于用户线程。 
用户线程和守护线程的区别是: 
(1)守护线程在调用start()运行之前,要调用setDaemon(true)才可以设置为守护线程; 
(2)守护线程中创建的子线程均为守护线程,即便没有设置setDaemon(true); 
(3)如果所有用户线程都退出,那么守护线程也会自动退出;

2 下面用两个例子测试以下守护线程的两个特性: 
(1)所有用户线程退出以后,守护线程也会退出。

public static class DaemonThread extends Thread {
        public DaemonThread(String name) {
            super(name);
        }
        @Override
        public void run() {
            super.run();

            int count = 0;
            while (true) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {

                } finally {
                    System.out.println(count++);
                }
            }
        }
    }

    public static void main(String[] args) {
        Thread thread = new DaemonThread("thread-1");
        //thread.setDaemon(true);
        thread.start();
        System.out.println("main thread exit");
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

上面代码注释掉setDaemon一行以后,两个线程均为用户线程,打印结果如下:

 main thread exit
0
1
2
3
4
5
6
7
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

虽然main线程退出了,但是thread-1线程由于执行while(true)循环,会一直执行下去,永远也无法退出。

如果不注释setDaemon,测试结果为:

main thread exit

 
 
  • 1
  • 2
  • 1
  • 2

此时,由于main线程退出,thread-1有可能根本没有执行(也有可能执行几次)就退出了。 
由此可以确认:当所有用户线程退出后,守护线程也会退出执行。利用这个特性,可以设计后台执行的垃圾清理任务,缓存过期删除等后台任务。

(2)守护线程创建的子线程也是守护线程,即便子线程没有调用setDaemon(true)显示的设置为守护线程 
测试代码如下:

public class test  {

    public static class ChildDaemonThread extends Thread {
        public ChildDaemonThread(String name) {
            super(name);
        }

        @Override
        public void run() {
            super.run();

            System.out.println(this.getName() + ": isDaemon = " + isDaemon());
        }
    }

    public static class DaemonThread extends Thread {
        public DaemonThread(String name) {
            super(name);
        }
        @Override
        public void run() {
            super.run();

            System.out.println(this.getName() + ": isDaemon = " + isDaemon());
            new ChildDaemonThread("ChildDaemonThread").start();
        }
    }

    public static void main(String[] args) {
        Thread thread = new DaemonThread("DaemonThread");
        thread.setDaemon(true);
        thread.start();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {

        }
        System.out.println("main thread exit");
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

测试结果如下:

DaemonThread: isDaemon = true
ChildDaemonThread: isDaemon = true
main thread exit
 
 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

可以看到守护线程中创建的线程也为守护线程,即便没有显示的调用setDaemon(true)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值