Java DaemonThread(守护线程)
Java中有两类线程:User Thread(用户线程)、Daemon Thread(守护线程) 。
只要当前JVM实例中尚存在任何一个非守护线程没有结束,守护线程就全部工作;当最后一个非守护线程结束时,守护线程就会随着JVM一同结束工作。
GC (垃圾回收器)就是一个最典型的守护线程。它始终在低级别的状态中运行,用于实时监控和管理系统中的可回收资源。
下面看4个例子
(1)主线程结束,JVM会等到User Thread(用户线程)执行完毕才退出。这说明在Java中创建的线程默认是User Thread(用户线程)。
package com.demo.test;
public class DeamonThreadDemo {
public static void main(String[] args) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("user thread end, isDaemon = " + Thread.currentThread().isDaemon());
}
});
//thread.setDaemon(true);
thread.start();
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
System.out.println("JVM is close");
}
}));
System.out.println("main end");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
输出
main end
thread end, isDaemon = false
JVM is close
- 1
- 2
- 3
(2)主线程结束,JVM并没有等待Daemon Thread(守护线程)执行结束就直接退出了。此时Daemon Thread(守护线程)被终止。
同”例(1)”的代码,取消掉thread.setDaemon(true);
的注释。
输出
main end
JVM is close
- 1
- 2
(3)在Daemon Thread(守护线程)中新建一个线程并启动,发现JVM在主线程执行结束时就退出了,并没有等待Daemon Thread(守护线程)中创建的新线程执行结束,这说明在Daemon Thread(守护线程)中创建的线程默认是Daemon Thread(守护线程)。
package com.demo.test;
public class DeamonThreadDemo {
public static void main(String[] args) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
Thread t = new Thread(new Runnable(){
@Override
public void run() {
System.out.println("child daemon thread start, isDaemon = " + Thread.currentThread().isDaemon());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("child daemon thread end, isDaemon = " + Thread.currentThread().isDaemon());
}
});
//t.setDaemon(false);
t.start();
System.out.println("daemon thread end");
}
});
thread.setDaemon(true);
thread.start();
try {
//这里休眠是为了保证能正常创建"child daemon thread"
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
System.out.println("JVM is close");
}
}));
System.out.println("main end");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
输出
child daemon thread start, isDaemon = true
daemon thread end, isDaemon = true
main end
JVM is close
- 1
- 2
- 3
- 4
(4)在Daemon Thread(守护线程)中创建的新线程虽然默认也是Daemon Thread(守护线程),但可以通过设置让其变为User Thread(用户线程)
同”例(3)”的代码,取消掉t.setDaemon(true);
的注释。
输出
child daemon thread start, isDaemon = false
daemon thread end, isDaemon = true
main end
child daemon thread end
JVM is close
- 1
- 2
- 3
- 4
- 5
文章来源: blog.csdn.net,作者:福州-司马懿,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/chy555chy/article/details/53037300
- 点赞
- 收藏
- 关注作者
评论(0)