本帖最后由 张洪慊 于 2013-3-22 15:10 编辑
- class StopThread implements Runnable
- {
- private boolean flag=true;
- public synchronized void run()
- {
- while(flag)
- {
-
- System.out.println(Thread.currentThread().getName()+"....run...");
- }
- }
- public void changeFlag()
- {
- flag=false;
-
- }
- }
- class setDaemonDemo
- {
- public static void main(String[] args)
- {
- StopThread s=new StopThread();
- Thread t1=new Thread(s);
- Thread t2=new Thread(s);
- t1.setDaemon(true);
- t2.setDaemon(true);//on为true 该线程被标记为守护线程
- t1.start();
- t2.start();
- int x=0;
- while(true)
- {
- if(x++==60)
- {
-
- break;
- }
- System.out.println(Thread.currentThread().getName()+"...."+x);
- }
- System.out.println(Thread.currentThread().getName()+"....over");
- }
- }
复制代码
主线程已经"over",守护线程又执行了一段?难道说,JVM退出需要一点时间?? 好吧,求详解.
|