您好,登錄后才能下訂單哦!
本文實例講述了Java守護線程用法。分享給大家供大家參考,具體如下:
一 點睛
針對守護線程,只要有一個用戶線程在執行,這個進程就不會結束。
當線程中只剩下守護線程時,JVM會自動退出,反之,如果還有其他任何用戶線程存在,JVM都不會退出。
二 代碼
public class ThreadDaemon { public static void main( String args[] ) { ThreadTest t = new ThreadTest(); Thread tt = new Thread( t ); tt.setDaemon( true ); // 設置后臺運行 tt.start( ); try { // 睡眠1毫秒,避免可能出現的沒有輸出的現象 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." ); } } }
三 運行
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相關內容感興趣的讀者可查看本站專題:《Java進程與線程操作技巧總結》、《Java數據結構與算法教程》、《Java操作DOM節點技巧總結》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。