根据我所知道的,一般程序一直要运行到用户线程结束为止,而不管daemon线程是不是存在,一般daemon线程是为拥护现成提供服务的!我也希望那位高人能例一个小例子详细说明他的用法·

解决方案 »

  1.   

    Daemon threadsA “daemon” thread is one that is supposed to provide a general service in the background as long as the program is running, but is not part of the essence of the program. Thus, when all of the non-daemon threads complete, the program is terminated. Conversely, if there are any non-daemon threads still running, the program doesn’t terminate. There is, for instance, a thread that runs main( ). Comment
    You can find out if a thread is a daemon by calling isDaemon( ), and you can turn the “daemonhood” of a thread on and off with setDaemon( ). If a thread is a daemon, then any threads it creates will automatically be daemons. Comment
    The following example demonstrates daemon threads:
    //: c14:Daemons.java
    // Daemonic behavior.
    // {RunByHand}
    import java.io.*;class Daemon extends Thread {
      private static final int SIZE = 10;
      private Thread[] t = new Thread[SIZE];
      public Daemon() { 
        setDaemon(true);
        start();
      }
      public void run() {
        for(int i = 0; i < SIZE; i++)
          t[i] = new DaemonSpawn(i);
        for(int i = 0; i < SIZE; i++)
          System.out.println(
            "t[" + i + "].isDaemon() = " 
            + t[i].isDaemon());
        while(true) 
          yield();
      }
    }class DaemonSpawn extends Thread {
      public DaemonSpawn(int i) {
        System.out.println(
          "DaemonSpawn " + i + " started");
        start();
      }
      public void run() {
        while(true) 
          yield();
      }
    }public class Daemons {
      public static void main(String[] args) 
      throws IOException {
        Thread d = new Daemon();
        System.out.println(
          "d.isDaemon() = " + d.isDaemon());
        // Allow the daemon threads to
        // finish their startup processes:
        System.out.println("Press any key");
        System.in.read();
      }
    } ///:~The Daemon thread sets its daemon flag to “true” and then spawns a bunch of other threads to show that they are also daemons. Then it goes into an infinite loop that calls yield( ) to give up control to the other processes. In an earlier version of this program, the infinite loops would increment int counters, but this seemed to bring the whole program to a stop. Using yield( ) makes the program quite peppy. Comment
    There’s nothing to keep the program from terminating once main( ) finishes its job, since there are nothing but daemon threads running. So that you can see the results of starting all the daemon threads, System.in is set up to read so the program waits for a keypress before terminating. Without this you see only some of the results from the creation of the daemon threads. (Try replacing the read( ) code with sleep( ) calls of various lengths to see this behavior.) Comment
                                             ---thinking in java 3rd edition
      

  2.   

    后台线程 如果jvm没有除了后台线程以外的其它线程 jvm将关闭