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