?? testthreaddaemon.java
字號:
import java.util.*;
public class TestThreadDaemon {
public static void main(String args[]) {
Thread t1 = new Daemon();
System.out.println("Main End");
}
}
class Daemon extends Thread {
static int id = 0;
Daemon(){
id ++;
setDaemon(true);
start();
if( id <5 ) new Daemon();
}
public void run() {
System.out.println("Start");
for(int i=0; i<10; i++ ){
System.out.print( "\n-----" + i + "------" + new Date() +"\n");
//try{ Thread.sleep(1000); } catch( InterruptedException e ){}
//yield();
}
}
}
/*
9.2.4 Daemon線程
線程有兩種,一類是Daemon線程,一類是非Daemon線程。在Java程序中,若還有非Demon線程,則整個程序就不會結束;而Daemon線程,可以在整個程序結束后繼續運行,所以Demon線程可以用于后臺服務程序。
通過調用isDaemon(),可檢查一個線程是不是一個Daemon;用setDaemon (boolean flg)方法可以將一個線程設為Daemon線程。在一個Daemon線程中創建的子線程,也自動是Daemon線程。
*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -