码迷,mamicode.com
首页 > 编程语言 > 详细

什么是后台(守护)线程?

时间:2015-07-17 18:27:29      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

所谓的后台(daemon)线程,也叫守护线程,是指程序在运行的时候,在后台提供一种通用服务的线程(例如:守护线程GC),并且,这种线程并不属于程序中不可或缺的部分;因此当所有的非后台线程结束时,程序也就终止了,同时杀死所有的后台线程。相反,只要有任何非后台线程(例如:非守护线程main())还存在,程序就不会终止。后台线程具有最低的优先级。

setDaemon(boolean on)方法可以方便的设置线程的Daemon模式,true为Daemon模式,默认(false)为User模式。setDaemon(boolean on)方法必须在线程启动(start())之前调用,当线程正在运行时调用会产生异常。

isDaemon方法将测试该线程是否为守护线程。注意:当你在一个守护线程中产生了其他线程,那么这些新产生的线程不用设置Daemon属性,都将是守护线程,用户线程同样。  

 

守护线程与普通线程的唯一区别是:守护线程就是main同生共死,当main退出,它将终止而普通线程是在任务执行结束才停止

普通线程代码:

技术分享
 1 package com.mianshi.easy;
 2 public class StartRunTest {
 3     /**
 4      * 后台(守护)线程实验
 5      */
 6     public static void main(String[] args){
 7         Thread thread=new ThreadDemo();
 8 
 9         //1、为什么没有打印出100句呢?因为我们将thread线程设置为了daemon(守护)线程,程序中只有守护线程存在的时候,是可以退出的,所以只打印了七句便退出了
10         //2、当java虚拟机中有守护线程在运行的时候,java虚拟机会关闭。当所有常规线程运行完毕以后,
11         //守护线程不管运行到哪里,虚拟机都会退出运行。所以你的守护线程最好不要写一些会影响程序的业务逻辑。否则无法预料程序到底会出现什么问题
12         //thread.setDaemon(true);
13         thread.start();
14     }
15     
16     public static class ThreadDemo extends Thread{
17         @Override
18         public void run() {
19             for (int i = 0; i < 100; i++) {
20                 System.out.println("This is a Thread test"+i);
21             }
22         }
23     }
24 }
View Code

结果:

技术分享
This is a Thread test0
This is a Thread test1
This is a Thread test2
This is a Thread test3
This is a Thread test4
This is a Thread test5
This is a Thread test6
This is a Thread test7
This is a Thread test8
This is a Thread test9
This is a Thread test10
This is a Thread test11
This is a Thread test12
This is a Thread test13
This is a Thread test14
This is a Thread test15
This is a Thread test16
This is a Thread test17
This is a Thread test18
This is a Thread test19
This is a Thread test20
This is a Thread test21
This is a Thread test22
This is a Thread test23
This is a Thread test24
This is a Thread test25
This is a Thread test26
This is a Thread test27
This is a Thread test28
This is a Thread test29
This is a Thread test30
This is a Thread test31
This is a Thread test32
This is a Thread test33
This is a Thread test34
This is a Thread test35
This is a Thread test36
This is a Thread test37
This is a Thread test38
This is a Thread test39
This is a Thread test40
This is a Thread test41
This is a Thread test42
This is a Thread test43
This is a Thread test44
This is a Thread test45
This is a Thread test46
This is a Thread test47
This is a Thread test48
This is a Thread test49
This is a Thread test50
This is a Thread test51
This is a Thread test52
This is a Thread test53
This is a Thread test54
This is a Thread test55
This is a Thread test56
This is a Thread test57
This is a Thread test58
This is a Thread test59
This is a Thread test60
This is a Thread test61
This is a Thread test62
This is a Thread test63
This is a Thread test64
This is a Thread test65
This is a Thread test66
This is a Thread test67
This is a Thread test68
This is a Thread test69
This is a Thread test70
This is a Thread test71
This is a Thread test72
This is a Thread test73
This is a Thread test74
This is a Thread test75
This is a Thread test76
This is a Thread test77
This is a Thread test78
This is a Thread test79
This is a Thread test80
This is a Thread test81
This is a Thread test82
This is a Thread test83
This is a Thread test84
This is a Thread test85
This is a Thread test86
This is a Thread test87
This is a Thread test88
This is a Thread test89
This is a Thread test90
This is a Thread test91
This is a Thread test92
This is a Thread test93
This is a Thread test94
This is a Thread test95
This is a Thread test96
This is a Thread test97
This is a Thread test98
This is a Thread test99
View Code

可见,在主线程中启动了默认的普通线程,此后主线程没有任何的执行语句,开始等待子线程执行,子线程得到CPU执行权后,开始执行run()方法,打印完100句话后,线程执行完成并终止,此时,主线程也会结束。程序中所有的非守护线程执行完成,程序结束。

 

守护线程代码:

 

技术分享
 1 package com.mianshi.easy;
 2 public class StartRunTest {
 3     /**
 4      * 后台(守护)线程实验
 5      */
 6     public static void main(String[] args){
 7         Thread thread=new ThreadDemo();
 8 
 9         //1、为什么没有打印出100句呢?因为我们将thread线程设置为了daemon(守护)线程,程序中只有守护线程存在的时候,是可以退出的,所以只打印了七句便退出了
10         //2、当java虚拟机中有守护线程在运行的时候,java虚拟机会关闭。当所有常规线程运行完毕以后,
11         //守护线程不管运行到哪里,虚拟机都会退出运行。所以你的守护线程最好不要写一些会影响程序的业务逻辑。否则无法预料程序到底会出现什么问题
12         thread.setDaemon(true);
13         thread.start();
14     }
15     
16     public static class ThreadDemo extends Thread{
17         @Override
18         public void run() {
19             for (int i = 0; i < 100; i++) {
20                 System.out.println("This is a Thread test"+i);
21             }
22         }
23     }
24 }
View Code

 

结果:

技术分享
1       
View Code

此时,进入主线程,创建了一个线程,随后将它设置为后台线程,并调用start()方法,使子线程就绪,但此时,程序中没有任何非后台线程,所以,主线程可以结束,程序也会停止。

 

什么是后台(守护)线程?

标签:

原文地址:http://www.cnblogs.com/gongxing/p/4654924.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!