- package thread;
- /*
- * Thread.setdaemon(),守护线程/用户线程-------也就是后台线程
- * 一,后台线程随”最后一个前台线程“的停止而停止
- * 二,在线程开始前调用
- */
- public class Setdaemon {
- public static void main(String args[]){
- Setdaemon_class s=new Setdaemon_class();
- Thread t=new Thread(s);
- Thread t1=new Thread(s);
- t.setDaemon(true);//在线程开始前进行调用
- // t1.setDaemon(true);//如果这里不标记为守护线程,那他就是前台线程
- t.start();
- t1.start();
- for (int i = 0; i < 33; i++) {
- System.out.println(i);
- }
- }
- }
- class Setdaemon_class implements Runnable{
- private int count;
- public void run(){
- while(true){
- System.out.println(Thread.currentThread().getName()+"------setdaemon(true)-----------"+count++);
- }
- }
- }
from : http://blog.csdn.net/wjw_java/article/details/7912611