日期:2014-05-20  浏览次数:20714 次

高手帮忙看个问题
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


------解决方案--------------------
你把System.out.println("d.isDaemon() = " + d.isDaemon()); 去掉试试