Java并發(fā)編程:守護線程
在Java中有兩類線程:用戶線程 (User Thread)、守護線程 (Daemon Thread)。
所謂守護線程,是指在程序運行的時候在后臺提供一種通用服務(wù)的線程,比如垃圾回收線程就是一個很稱職的守護者,并且這種線程并不屬于程序中不可或缺的部分。因此,當所有的非守護線程結(jié)束時,程序也就終止了,同時會殺死進程中的所有守護線程。反過來說,只要任何非守護線程還在運行,程序就不會終止。
用戶線程和守護線程兩者幾乎沒有區(qū)別,***的不同之處就在于虛擬機的離開:如果用戶線程已經(jīng)全部退出運行了,只剩下守護線程存在了,虛擬機也就退出了。 因為沒有了被守護者,守護線程也就沒有工作可做了,也就沒有繼續(xù)運行程序的必要了。
將線程轉(zhuǎn)換為守護線程可以通過調(diào)用Thread對象的setDaemon(true)方法來實現(xiàn)。在使用守護線程時需要注意一下幾點:
(1) thread.setDaemon(true)必須在thread.start()之前設(shè)置,否則會跑出一個IllegalThreadStateException異常。你不能把正在運行的常規(guī)線程設(shè)置為守護線程。
(2) 在Daemon線程中產(chǎn)生的新線程也是Daemon的。
(3) 守護線程應(yīng)該永遠不去訪問固有資源,如文件、數(shù)據(jù)庫,因為它會在任何時候甚至在一個操作的中間發(fā)生中斷。
代碼示例:
- 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); //必須在啟動線程前調(diào)用
- 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();
- }
- }
- }
運行結(jié)果:
- 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.
以上結(jié)果說明了守護線程中產(chǎn)生的新線程也是守護線程。
如果將mian函數(shù)中的TimeUnit.SECONDS.sleep(1);注釋掉,運行結(jié)果如下:
- 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.
以上結(jié)果說明了如果用戶線程已經(jīng)全部退出運行了,只剩下守護線程存在了,虛擬機也就退出了。下面的例子也說明了這個問題。
代碼示例:
- 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 ?");
- }
- }
- }
運行結(jié)果:
start ADaemon...
如果將main函數(shù)中的t.setDaemon(true);注釋掉,運行結(jié)果如下:
start ADaemon...
This shoud be always run ?
原文鏈接:http://www.cnblogs.com/luochengor/archive/2011/08/11/2134818.html
【編輯推薦】