当前位置:网站首页>Daemon and user threads
Daemon and user threads
2022-06-30 06:58:00 【A Xiaoming】
1. Basic knowledge of :
Threads are divided into user threads and guard threads
The virtual machine must make sure that the user thread is finished ( That is, the execution of the user thread ends , Virtual machine will end )
The virtual machine doesn't have to wait for the daemons to finish executing ( That is, the daemon thread and the virtual machine exist or die together )
Common daemon threads such as , Record the operation log in the background , Monitor memory , Garbage collection waiting …
2. Example
/** Daemons and user threads */
public class TestDaemon {
public static void main(String[] args) {
God god = new God();
Person p = new Person();
Thread daemon = new Thread(god);
daemon.setDaemon(true); // Set to daemons , Live or die with the virtual machine , The default is false, That is, the user thread is assumed by default
daemon.start();
Thread common = new Thread(p);
common.start();
}
}
class God implements Runnable{
@Override
public void run() {
while (true) {
System.out.println(" God is watching over you ....");
}
}
}
class Person implements Runnable{
@Override
public void run() {
for (int i = 1;i<35000;i++){
System.out.println(" Live happily every day ....");
}
System.out.println("===========Goodbye world...");
}
}
Execution results :
The user thread and the daemon thread execute together 
End of user thread execution , Daemon thread continues execution 
Execute until the virtual machine stops .
边栏推荐
猜你喜欢

记录一次腾讯测试开发工程师自动化接口测试实践经验

原来你是这样的数组,终于学会了

Mysql5.7 compressed version installation tutorial

Google Earth engine (GEE) - Murray global tidal wetland change V1 (1999-2019) data set

First experience of Galaxy Kirin

Xshell传输文件

1.9 - Classification of memory

Jingwei Hengrun won the 10ppm quality award of paccar group again

GO安装以及配置(1)
![[my creation anniversary] one year anniversary essay](/img/98/f9305894747687465f86354fe08500.png)
[my creation anniversary] one year anniversary essay
随机推荐
Solr search
Fastapi learning Day1
SOC_ SD_ CLK
Introduction to programming ape (11) -- structure
Basic questions (I)
元宇宙由哪些底层技术支撑?
成品升级程序
写一个C程序判断系统是大端字节序还是小端字节序
Write a C program to judge whether the system is large end byte order or small end byte order
15 minutes learn to use JWT
The 40g high-efficiency cloud disk purchased by Alibaba cloud is only 20g attached
How to convert XML to JSON
0基础转行软件测试,如何实现月薪9.5k+
[Hot100]回文子串 与 最长回文子串
MySQL优化:从十几秒优化到三百毫秒
Records of problems solved (continuously updated)
RT thread Kernel Implementation (VI): time slice
RT thread application
ETL为什么经常变成ELT甚至LET?
[my creation anniversary] one year anniversary essay