时间:2021-05-20
本文实例讲述了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程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了Java线程之守护线程(Daemon)用法。分享给大家供大家参考。具体如下:守护线程(Daemon)Java有两种Thread:“守护线程Daem
本文实例分析了Java线程调度之线程休眠用法。分享给大家供大家参考。具体分析如下:Java线程调度是Java多线程的核心,只有良好的调度,才能充分发挥系统的性能
Java中有两类线程:UserThread(用户线程)、DaemonThread(守护线程)用户线程即运行在前台的线程,而守护线程是运行在后台的线程。守护线程作
本文实例讲述了Python守护线程用法。分享给大家供大家参考,具体如下:如果你设置一个线程为守护线程,就表示你在说这个线程是不重要的,在进程退出的时候,不用等待
前言在Java中有两类线程:UserThread(用户线程)、DaemonThread(守护线程)用个比较通俗的比如,任何一个守护线程都是整个JVM中所有非守护