Java守护线程用法实例分析
本文实例讲述了Java守护线程用法。分享给大家供大家参考,具体如下:
一点睛
针对守护线程,只要有一个用户线程在执行,这个进程就不会结束。
当线程中只剩下守护线程时,JVM会自动退出,反之,如果还有其他任何用户线程存在,JVM都不会退出。
二代码
publicclassThreadDaemon { publicstaticvoidmain(Stringargs[]) { ThreadTestt=newThreadTest(); Threadtt=newThread(t); tt.setDaemon(true);//设置后台运行 tt.start(); try {//睡眠1毫秒,避免可能出现的没有输出的现象 Thread.sleep(1); } catch(InterruptedExceptione) { e.printStackTrace(); } } } classThreadTestimplementsRunnable { publicvoidrun() { for(inti=0;true;++i) { System.out.println(i+""+Thread.currentThread().getName() +"isrunning."); } } }
三运行
0 Thread-0isrunning.
1 Thread-0isrunning.
2 Thread-0isrunning.
3 Thread-0isrunning.
4 Thread-0isrunning.
5 Thread-0isrunning.
6 Thread-0isrunning.
7 Thread-0isrunning.
8 Thread-0isrunning.
9 Thread-0isrunning.
10 Thread-0isrunning.
11 Thread-0isrunning.
12 Thread-0isrunning.
13 Thread-0isrunning.
14 Thread-0isrunning.
15 Thread-0isrunning.
16 Thread-0isrunning.
17 Thread-0isrunning.
18 Thread-0isrunning.
19 Thread-0isrunning.
20 Thread-0isrunning.
21 Thread-0isrunning.
22 Thread-0isrunning.
23 Thread-0isrunning.
24 Thread-0isrunning.
25 Thread-0isrunning.
26 Thread-0isrunning.
27 Thread-0isrunning.
28 Thread-0isrunning.
更多java相关内容感兴趣的读者可查看本站专题:《Java进程与线程操作技巧总结》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。