例子如下
public class ThreadTest extends Thread{ @Override
public void run() {
for(int i=0;i<100;i++){
System.out.println("i="+i+"["+System.currentTimeMillis()+"]"); }

} /**
 * @param args
 * @throws InterruptedException 
 */
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.out.println("start"+System.currentTimeMillis());
ThreadTest t=new ThreadTest();
t.setDaemon(true);
t.start();

System.out.println("end:"+System.currentTimeMillis());
}}结果:start1287066481651
end:1287066481667
i=0[1287066481667]
i=1[1287066481667]
i=2[1287066481667]
i=3[1287066481667]
i=4[1287066481667]
i=5[1287066481667]
i=6[1287066481667]
i=7[1287066481667]
i=8[1287066481667]
i=9[1287066481667]
i=10[1287066481667]
i=11[1287066481667]
i=12[1287066481667]
i=13[1287066481667]
i=14[1287066481667]
i=15[1287066481667]
i=16[1287066481667]
i=17[1287066481667]
i=18[1287066481667]
i=19[1287066481667]
i=20[1287066481667]
i=21[1287066481667]
i=22[1287066481667]
i=23[1287066481667]
i=24[1287066481667]
i=25[1287066481667]
i=26[1287066481667]
i=27[1287066481667]
i=28[1287066481667]
i=29[1287066481667]
i=30[1287066481667]
i=31[1287066481667]
i=32[1287066481667]
i=33[1287066481667]
i=34[1287066481667]
i=35[1287066481667]
i=36[1287066481667]
i=37[1287066481667]
i=38[1287066481667]
i=39[1287066481667]
i=40[1287066481667]
i=41[1287066481667]
i=42[1287066481667]
i=43[1287066481667]
i=44[1287066481667]
i=45[1287066481667]
i=46[1287066481667]
i=47[1287066481667]
i=48[1287066481667]
i=49[1287066481667]
i=50[1287066481667]
i=51[1287066481667]
i=52[1287066481667]
i=53[1287066481667]
i=54[1287066481667]
i=55[1287066481667]
i=56[1287066481667]
i=57[1287066481667]
i=58[1287066481667]
i=59[1287066481667]
i=60[1287066481667]
i=61[1287066481667]
i=62[1287066481667]
i=63[1287066481667]
i=64[1287066481667]
i=65[1287066481667]
i=66[1287066481667]
i=67[1287066481667]
i=68[1287066481667]
i=69[1287066481667]
i=70[1287066481667]
i=71[1287066481667]
i=72[1287066481667]
i=73[1287066481667]
i=74[1287066481667]
i=75[1287066481667]
i=76[1287066481667]
i=77[1287066481667]
i=78[1287066481667]
i=79[1287066481667]
i=80[1287066481667]
i=81[1287066481667]
i=82[1287066481667]
i=83[1287066481667]
i=84[1287066481667]
i=85[1287066481667]
i=86[1287066481667]
i=87[1287066481667]
i=88[1287066481667]
i=89[1287066481667]
i=90[1287066481667]
i=91[1287066481667]
i=92[1287066481667]
i=93[1287066481667]
i=94[1287066481667]
i=95[1287066481667]
i=96[1287066481667]
i=97[1287066481667]
i=98[1287066481667]
i=99[1287066481667]照理说Daemon线程应该在前台线程死掉就自动退出了,可是结果显示它还是运行完毕了.

解决方案 »

  1.   

    是因为你的CPU跑得太快了,线程还没来得及切换
    在run()方法里加上:
    try{
       Thread.sleep(100);
    }catch(Exception e){
      e.printStackTrace();
    }
      

  2.   


    public class ThreadTest extends Thread{
    public void run() {
            for(int i=0;i<100;i++){
             try {
    Thread.sleep(1);//这里休息1个毫秒
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
                System.out.println("i="+i+"["+System.currentTimeMillis()+"]"); 
            } 
        }
        /**
         * @param args
         * @throws InterruptedException 
         */
        public static void main(String[] args) throws InterruptedException {
          System.out.println("start"+System.currentTimeMillis());
             ThreadTest t=new ThreadTest();
             t.setDaemon(true);
             t.start();
             System.out.println("end:"+System.currentTimeMillis());
             //jvm退出的时候会执行下面的代码,但是总会需要时间的,所以让线程sleep()一小下
               //就看得比较明显了
             Runtime.getRuntime().addShutdownHook(new Thread(){
                @Override
             public void run() {
                  // TODO Auto-generated method stub
              System.out.println("JVM Exit!");
              }
            });
        }
    }
      

  3.   

    修改下你的线程打印代码,然其每打印一次sleep 2 ms.你就可以看到你想要的结果了