守護線程(Daemon Thread)

在Java中有兩類線程:用戶線程 (User Thread)、守護線程 (Daemon Thread)。 java

所謂守護 線程,是指在程序運行的時候在後臺提供一種通用服務的線程,好比垃圾回收線程就是一個很稱職的守護者,而且這種線程並不屬於程序中不可或缺的部分。所以,當全部的非守護線程結束時,程序也就終止了,同時會殺死進程中的全部守護線程。反過來講,只要任何非守護線程還在運行,程序就不會終止。數據庫

用戶線程和守護線程二者幾乎沒有區別,惟一的不一樣之處就在於虛擬機的離開:若是用戶線程已經所有退出運行了,只剩下守護線程存在了,虛擬機也就退出了。 由於沒有了被守護者,守護線程也就沒有工做可作了,也就沒有繼續運行程序的必要了。函數

將線程轉換爲守護線程能夠經過調用Thread對象的setDaemon(true)方法來實現。在使用守護線程時須要注意一下幾點:spa

 

(1) thread.setDaemon(true)必須在thread.start()以前設置,不然會跑出一個IllegalThreadStateException異常。你不能把正在運行的常規線程設置爲守護線程。 線程

(2) 在Daemon線程中產生的新線程也是Daemon的。code

(3) 守護線程應該永遠不去訪問固有資源,如文件、數據庫,由於它會在任什麼時候候甚至在一個操做的中間發生中斷。對象

代碼示例:blog

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 ?

相關文章
相關標籤/搜索