测试守护线程的小例子

来源:互联网 发布:免费开源java即时通讯 编辑:程序博客网 时间:2024/06/05 09:40
public class Test {
/** 
* User:liluzhong 
* Date: Dec 10, 2017 
* Time: 7:16:55 PM 
*/  
   public static void main(String[] args) throws Exception{  
       DaemonThread t = new DaemonThread();  
       t.setDaemon(true);//this is set t thread as a daemon thread.  
       t.start();  
       Thread.sleep(2000);  
       System.out.println("main thread exit.");  
   }  
}  
 
class DaemonThread extends Thread {  
   @Override  
   public void run() {  
       for(int i = 0; i < 10; i++) {  
           try {  
               Thread.sleep(1000);  
           } catch (InterruptedException e) {  
               e.printStackTrace();  
           }  
           System.out.println("i=" + i);  
       }  
   }  
}