/**
* 守護線程在沒有用戶線程可服務時自動離開
* @author 五斗米
* @blog http://blog.csdn.net/mq612
*/
public class TestMain4 extends Thread {
public TestMain4() {
}
/**
* 線程的run方法,它將和其他線程同時運行
*/
public void run() {
for(int i = 1; i <= 100; i++){
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
System.out.println(i);
}
}
public static void main(String [] args){
TestMain4 test = new TestMain4();
test.setDaemon(true);
test.start();
System.out.println("isDaemon = " + test.isDaemon());
try {
System.in.read(); // 接受輸入,使程序在此停頓,一旦接收到用戶輸入,main線程結束,守護線程自動結束
} catch (IOException ex) {
ex.printStackTrace();
}
}
}