import java.io.IOException;
public class Trr extends Thread{
public Trr(String str ){
this.str=str;

}
public static void main(String[] args){
Trr a=new Trr("The frist");
Trr b=new Trr("The second");
a.setDaemon(true);
//b.setDaemon(true);
a.start();//这是守护线程
b.start();//这是用户线程


try{
System.in.read();
System.out.println("Enter pressed...\n");
}catch(IOException e){
System.out.println(e);
}
System.out.println("Ending main()");
return ;
}
public void run(){
try{
    while(true){
     System.out.println(str);
        sleep(100);
    }
}catch(InterruptedException e){
  e.printStackTrace();
}
}
private String str;
}
思路:我创建了一个守护线程和一个用户线程。并在mian()函数里做了个read()方法, 当敲入回车键控制主线程的结束。
问题来了 当主线程结束后,照理来说 应该守护线程也结束,只剩用户线程继续运行。可我发现守护线程还在运行。
这是怎么回事啊~

解决方案 »

  1.   

    According to Webster's, a daemon (variant of demon) is an attendant power or spirit. Daemon threads are typically used to perform services for your application/applet (such as loading the "fiddley bits"). The core difference between user threads and daemon threads is that the JVM will only shut down a program when all user threads have terminated. Daemon threads are terminated by the JVM when there are no longer any user threads running, including the main thread of execution. Use daemons as the minions they are. 从这段话看来
    只要有用户线程(主线程也是其中一种)没有结束, 守护进程就不会自动结束吧?
    并不是主线程结束守护进程就会结束的.
      

  2.   

    我企图找到更加令人信服的解释呢
    没找到-_-
    不过我也没有看到有说主线程结束守护线程就会结束的话呀~api里面说
    When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs: The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place. 
    All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.是不是也可以这样理解
    Thread是不会自动结束的
    而如果没有用户线程 jvm就会终止 这样相应的 守护线程也就结束了 
      

  3.   

    http://jguru.com/faq/view.jsp?EID=43724google 关键字: Daemon Thread
      

  4.   

    学习到了 呵呵 彪悍的测试 以前都是只测试过一个
    用1F的说明 改了下例子 果然如此
    import java.io.IOException;
    public class Trr extends Thread{
    public Trr(String str,int i ){
    this.str=str;
    this.cnt=i;

    }
    public static void main(String[] args)throws Exception{
    Trr a=new Trr("The frist",-1);
    Trr b=new Trr("The second",25);
    a.setDaemon(true);
    //b.setDaemon(true);
    a.start();//这是守护线程
    b.start();//这是用户线程


            Thread.sleep(3000);
    System.out.println("Ending main()");
    return ;
    }
    public void run(){
    try{
        while(true){
         System.out.println(str);
            sleep(300);

    if((cnt--)==0)break;
        }
    }catch(InterruptedException e){
      e.printStackTrace();
    }
    }
    private String str;
    private int cnt=0;
    }
      

  5.   


    On the other hand we have Daemon threads. Typically these threads are service provider threads. They should not be used to run your program code but some system code. These thread run paralley to your code but survive on the mercy of the JVM. When JVM finds no user threads it stops and all daemon thread terminate instantly. 
    我认为上面这句 应该指所有的 包括别的方法建立的 ,因为是JVM findesThus one should never rely on daemon code to perform any program code.