Java中如何使用守護線程

今天就跟大家聊聊有關(guān)Java中如何使用守護線程,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供猇亭網(wǎng)站建設(shè)、猇亭做網(wǎng)站、猇亭網(wǎng)站設(shè)計、猇亭網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、猇亭企業(yè)網(wǎng)站模板建站服務(wù),十年猇亭做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。

一 點睛

針對守護線程,只要有一個用戶線程在執(zhí)行,這個進程就不會結(jié)束。

當(dāng)線程中只剩下守護線程時,JVM會自動退出,反之,如果還有其他任何用戶線程存在,JVM都不會退出。

二 代碼

public class ThreadDaemon{  public static void main( String args[] )  {   ThreadTest t = new ThreadTest();   Thread tt = new Thread( t );   tt.setDaemon( true ); // 設(shè)置后臺運行   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." );   }  }}

三 運行

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.

看完上述內(nèi)容,你們對Java中如何使用守護線程有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

分享文章:Java中如何使用守護線程
標(biāo)題URL:http://muchs.cn/article48/ghgoep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司、網(wǎng)站設(shè)計、Google、網(wǎng)站建設(shè)網(wǎng)站制作、商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站