本文實(shí)例講述了Java守護(hù)線程用法。分享給大家供大家參考,具體如下:
為九臺(tái)等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及九臺(tái)網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站設(shè)計(jì)、做網(wǎng)站、九臺(tái)網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
一 點(diǎn)睛
針對(duì)守護(hù)線程,只要有一個(gè)用戶線程在執(zhí)行,這個(gè)進(jìn)程就不會(huì)結(jié)束。
當(dāng)線程中只剩下守護(hù)線程時(shí),JVM會(huì)自動(dòng)退出,反之,如果還有其他任何用戶線程存在,JVM都不會(huì)退出。
二 代碼
public class ThreadDaemon { public static void main( String args[] ) { ThreadTest t = new ThreadTest(); Thread tt = new Thread( t ); tt.setDaemon( true ); // 設(shè)置后臺(tái)運(yùn)行 tt.start( ); try { // 睡眠1毫秒,避免可能出現(xiàn)的沒有輸出的現(xiàn)象 Thread.sleep( 1 ); } catch( InterruptedException e ) { e.printStackTrace(); } } } class ThreadTest implements Runnable { public void run() { for( int i = 0; true; ++i ) { System.out.println( i + " " + Thread.currentThread().getName() + " is running." ); } } }
三 運(yùn)行
0 Thread-0 is running.
1 Thread-0 is running.
2 Thread-0 is running.
3 Thread-0 is running.
4 Thread-0 is running.
5 Thread-0 is running.
6 Thread-0 is running.
7 Thread-0 is running.
8 Thread-0 is running.
9 Thread-0 is running.
10 Thread-0 is running.
11 Thread-0 is running.
12 Thread-0 is running.
13 Thread-0 is running.
14 Thread-0 is running.
15 Thread-0 is running.
16 Thread-0 is running.
17 Thread-0 is running.
18 Thread-0 is running.
19 Thread-0 is running.
20 Thread-0 is running.
21 Thread-0 is running.
22 Thread-0 is running.
23 Thread-0 is running.
24 Thread-0 is running.
25 Thread-0 is running.
26 Thread-0 is running.
27 Thread-0 is running.
28 Thread-0 is running.
更多java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java進(jìn)程與線程操作技巧總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。