在Java中有兩類線程:用戶線程(UserThread)、守護線程(DaemonThread)。
所謂守護線程,是指在程序運行的時候在后臺提供一種通用服務的線程,比如垃圾回收線程就是一個很稱職的守護者,并且這種線程并不屬于程序中不可或缺的部分。因此,當所有的非守護線程結束時,程序也就終止了,同時會殺死進程中的所有守護線程。反過來說,只要任何非守護線程還在運行,程序就不會終止。
用戶線程和守護線程兩者幾乎沒有區別,唯一的不同之處就在于虛擬機的離開:如果用戶線程已經全部退出運行了,只剩下守護線程存在了,虛擬機也就退出了。因為沒有了被守護者,守護線程也就沒有工作可做了,也就沒有繼續運行程序的必要了。
將線程轉換為守護線程可以通過調用Thread對象的setDaemon(true)方法來實現。在使用守護線程時需要注意一下幾點:
(1)thread.setDaemon(true)必須在thread.start()之前設置,否則會跑出一個IllegalThreadStateException異常。你不能把正在運行的常規線程設置為守護線程。
(2)在Daemon線程中產生的新線程也是Daemon的。
(3)守護線程應該永遠不去訪問固有資源,如文件、數據庫,因為它會在任何時候甚至在一個操作的中間發生中斷。
代碼示例:
- import java.util.concurrent.TimeUnit;
- /**
- * 守護線程
- */
- public class Daemons {
- /**
- * @param args
- * @throws InterruptedException
- */
- public static void main(String[] args) throws InterruptedException {
- Thread d = new Thread(new Daemon());
- d.setDaemon(true); //必須在啟動線程前調用
- d.start();
- System.out.println("d.isDaemon() = " + d.isDaemon() + ".");
- TimeUnit.SECONDS.sleep(1);
- }
- }
- class DaemonSpawn implements Runnable {
- public void run() {
- while (true) {
- Thread.yield();
- }
- }
- }
- class Daemon implements Runnable {
- private Thread[] t = new Thread[10];
- public void run() {
- for (int i=0; i<t.length; i++) {
- t[i] = new Thread(new DaemonSpawn());
- t[i].start();
- System.out.println("DaemonSpawn " + i + " started.");
- }
- for (int i=0; i<t.length; i++) {
- System.out.println("t[" + i + "].isDaemon() = " +
- t[i].isDaemon() + ".");
- }
- while (true) {
- Thread.yield();
- }
- }
- }
|
運行結果:
- d.isDaemon() = true.
- DaemonSpawn 0 started.
- DaemonSpawn 1 started.
- DaemonSpawn 2 started.
- DaemonSpawn 3 started.
- DaemonSpawn 4 started.
- DaemonSpawn 5 started.
- DaemonSpawn 6 started.
- DaemonSpawn 7 started.
- DaemonSpawn 8 started.
- DaemonSpawn 9 started.
- t[0].isDaemon() = true.
- t[1].isDaemon() = true.
- t[2].isDaemon() = true.
- t[3].isDaemon() = true.
- t[4].isDaemon() = true.
- t[5].isDaemon() = true.
- t[6].isDaemon() = true.
- t[7].isDaemon() = true.
- t[8].isDaemon() = true.
- t[9].isDaemon() = true.
|
以上結果說明了守護線程中產生的新線程也是守護線程。
如果將mian函數中的TimeUnit.SECONDS.sleep(1);注釋掉,運行結果如下:
- d.isDaemon() = true.
- DaemonSpawn 0 started.
- DaemonSpawn 1 started.
- DaemonSpawn 2 started.
- DaemonSpawn 3 started.
- DaemonSpawn 4 started.
- DaemonSpawn 5 started.
- DaemonSpawn 6 started.
- DaemonSpawn 7 started.
- DaemonSpawn 8 started.
- DaemonSpawn 9 started.
|
以上結果說明了如果用戶線程已經全部退出運行了,只剩下守護線程存在了,虛擬機也就退出了。下面的例子也說明了這個問題。
代碼示例:
- import java.util.concurrent.TimeUnit;
- /**
- * Finally shoud be always run ?
- */
- public class DaemonsDontRunFinally {
- /**
- * @param args
- */
- public static void main(String[] args) {
- Thread t = new Thread(new ADaemon());
- t.setDaemon(true);
- t.start();
- }
- }
- class ADaemon implements Runnable {
- public void run() {
- try {
- System.out.println("start ADaemon...");
- TimeUnit.SECONDS.sleep(1);
- } catch (InterruptedException e) {
- System.out.println("Exiting via InterruptedException");
- } finally {
- System.out.println("This shoud be always run ?");
- }
- }
- }
|
運行結果:
start ADaemon...
如果將main函數中的t.setDaemon(true);注釋掉,運行結果如下:
start ADaemon...
This shoud be always run ?