大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
今天就跟大家聊聊有关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中如何使用守护线程有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。