多線程(守護線程)學習筆記
阿新 • • 發佈:2018-08-08
tac for lee 線程 () 單獨 true set int
setDaemon(), 設置一個線程為守護線程, 該線程不會單獨執行, 當其他非守護線程都執行結束後, 自動退出
*
Thread t1 = new Thread() {
public void run() {
for(int i = 0; i < 50; i++) {
System.out.println(getName() + "...aaaaaaaaaaaaaaaaaaaaaa");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread t2 = new Thread() {
public void run() {
for(int i = 0; i < 5; i++) {
System.out.println(getName() + "...bb");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t1.setDaemon(true); //將t1設置為守護線程
t1.start();
t2.start();
多線程(守護線程)學習筆記