1 用户线程和守护线程
Java虚拟机中,有两种线程:用户线程(User Thread)和守护线程(Daemon Thread)。
平时我们编写代码中的main函数所在的线程一般就是用户线程。
守护线程是相对于用户线程而言的。守护线程的优先级低于用户线程。
用户线程和守护线程的区别是:
(1)守护线程在调用start()运行之前,要调用setDaemon(true)才可以设置为守护线程;
(2)守护线程中创建的子线程均为守护线程,即便没有设置setDaemon(true);
(3)如果所有用户线程都退出,那么守护线程也会自动退出;
2 下面用两个例子测试以下守护线程的两个特性:
(1)所有用户线程退出以后,守护线程也会退出。
- 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一行以后,两个线程均为用户线程,打印结果如下:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
虽然main线程退出了,但是thread-1线程由于执行while(true)循环,会一直执行下去,永远也无法退出。
如果不注释setDaemon,测试结果为:
- 1
- 2
- 1
- 2
此时,由于main线程退出,thread-1有可能根本没有执行(也有可能执行几次)就退出了。
由此可以确认:当所有用户线程退出后,守护线程也会退出执行。利用这个特性,可以设计后台执行的垃圾清理任务,缓存过期删除等后台任务。
(2)守护线程创建的子线程也是守护线程,即便子线程没有调用setDaemon(true)显示的设置为守护线程
测试代码如下:
- 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
测试结果如下:
- 1
- 2
- 3
- 1
- 2
- 3
可以看到守护线程中创建的线程也为守护线程,即便没有显示的调用setDaemon(true)