?? daemon.java
字號(hào):
package book.thread;
/**
* Daemon(守護(hù))線程
* Daemon線程區(qū)別一般線程之處是:
* 只有虛擬機(jī)中的用戶線程(非Daimon線程)全部結(jié)束,Daemon線程就會(huì)立即結(jié)束,并且也不會(huì)調(diào)用finally里的語(yǔ)句。
* daemon線程所產(chǎn)生的所有線程都是daemon的
*/
public class Daemon {
static class MainThread extends Thread {
public void run() {
System.out.println("MainThread is daemon? " + this.isDaemon());
System.out.println("MainThread begin!");
//啟動(dòng)子線程
Thread sub1 = new SubThread();
//sub1線程為守護(hù)線程
sub1.setDaemon(true);
sub1.start();
try {
Thread.sleep(1000);
}catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("MainThread" + " finally");
}
System.out.println("MainThread end!");
}
}
static class SubThread extends Thread {
public void run() {
System.out.println("SubThread is daemon? " + this.isDaemon());
System.out.println("SubThread begin!");
int i = 0;
try {
while (i < 10) {
System.out.println("SubThread " + i++);
Thread.sleep(200);
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("SubThread finally");
}
System.out.println("SubThread end!");
}
}
public static void main(String[] args) {
System.out.println("Main begin!");
//默認(rèn)情況下mainThread是普通線程
Thread mainThread = new MainThread();
//啟動(dòng)mainThread線程
mainThread.start();
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println("Main end!");
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -