import java.io.*;class Daemon extends Thread {
  private Thread[] t = new Thread[10];
  public Daemon() {
    setDaemon(true);
    start();
  }
  public void run() {
    for(int i = 0; i < t.length; i++)
      t[i] = new DaemonSpawn(i);
    for(int i = 0; i < t.length; i++)
      System.out.println("t[" + i + "].isDaemon() = "
        + t[i].isDaemon());
    while(true)
      yield();
  }
}class DaemonSpawn extends Thread {
  public DaemonSpawn(int i) {
    start();
    System.out.println("DaemonSpawn " + i + " started");
  }
  public void run() {
    while(true)
      yield();
  }
}public class Daemons {
  public static void main(String[] args) throws Exception {
    Thread d = new Daemon();
    System.out.println("d.isDaemon() = " + d.isDaemon());
    // Allow the daemon threads to
    // finish their startup processes:
    Thread.sleep(1000);
  }
}-------------------------
上面这个程序为什么最先输出的是
d.isDaemon() = true

解决方案 »

  1.   

    输出是
    d.isDaemon() = true
    DaemonSpawn 0 started
    DaemonSpawn 1 started
    DaemonSpawn 2 started
    DaemonSpawn 3 started
    ................为什么System.out.println("d.isDaemon() = " + d.isDaemon()); 会比Thread d = new Daemon(); 
    先得到执行啊?
      

  2.   

    你把System.out.println("d.isDaemon() = " + d.isDaemon()); 去掉试试 
      

  3.   


    大哥,去掉当然不输出拉
    问题是为什么感觉System.out.println("d.isDaemon() = " + d.isDaemon()); 会比Thread d = new Daemon(); 
    先执行
      

  4.   

    main线程直接启动,比其它的线程都快一些。
    我改了下你的代码,你看看 就知道了。
    import java.io.*;class Daemons extends Thread {

    public static void main(String[] args) throws Exception {
    Thread d = new Daemons();
    System.out.println("d.isDaemon() = " + d.isDaemon());
    // Allow the daemon threads to 
    // finish their startup processes: 
    // Thread.sleep(1000);
    }

    private Thread[] t = new Thread[10]; public Daemons() {
    setDaemon(true);
    start();
    try {
    Thread.sleep(5000);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    } public void run() {
    for (int i = 0; i < t.length; i++)
    t[i] = new DaemonSpawn(i);
    for (int i = 0; i < t.length; i++)
    System.out.println("t[" + i + "].isDaemon() = " + t[i].isDaemon());
    while (true)
    yield();
    }
    }class DaemonSpawn extends Thread {
    public DaemonSpawn(int i) {
    start();
    System.out.println("DaemonSpawn " + i + " started");
    } public void run() {
    while (true)
    yield();
    }
    }
      

  5.   

    借这个帖子再提问一个问题吧,问题在注释处public class AlwaysEven {
      private int i;
      public void next() { i++; i++; }
      public int getValue() { return i; }
      public static void main(String[] args) {
        final AlwaysEven ae = new AlwaysEven();
        new Thread("Watcher") {
          public void run() {
            while(true) {
              int val = ae.getValue();
              if(val % 2 != 0) {
                System.out.println(val);   //为什么会进入到这里呢,难道线程是在next函数执行第一个i++的时候启动了?
                System.exit(0);
              }
            }
          }
        }.start();
        while(true)
          ae.next();
      }
    } ///:~
      

  6.   

    难道线程是在next函数执行第一个i++的时候启动了? 
    差不多
      

  7.   

    这个问题是关于线程的优先级问题,当你Thread d = new Daemon();创建d这个线程时并启动时可能就打印System.out.println("d.isDaemon() = " + d.isDaemon()); 这个语句了,关于线程的理解LZ最好是脑袋里有个时间差的观念,毕竟JVM比我们大脑要处理快的多,我也不好怎么说,LZ自己多看下这方面的书,这个时间差的观念就会有的!!!