当前位置:网站首页>Daemon threads and application scenarios
Daemon threads and application scenarios
2022-07-18 17:50:00 【GuochaoHN】
What is a guardian thread
Daemon threads are different from user threads , The user thread is the thread we created manually , The daemon thread is a thread that provides a general service in the background when the program is running .
I use my own vernacular to summarize :
Why is it called a daemon ? What is it guarding ?
It guards the thread that created this thread , So the creator is the protected object .
So how does the created guard thread guard ?
The daemons have the feature of automatically ending their life cycle , Non Guardian threads don't have
eg:
The guardian thread :
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(()-> {
while (true) {
try {
Thread.sleep(1000);
System.out.println(" I'm a child thread ( User threads .I am running");
} catch (Exception e) {
}
}
});
// Marked as a daemon thread
t1.setDaemon(true);
// Start thread
t1.start();
Thread.sleep(3000);
System.out.println(" Main thread execution completed ...");
}

You can find that after marking as a guard thread , Main thread destroy stop , The daemons are destroyed together ( Self destruction ).
eg:
Non-daemon thread :
Get rid of t1.setDaemon(true) Guardian mark effect 
therefore , When the main thread exits ,JVM And then it goes out of operation , The daemons are also recycled , Even if it's a dead circle . If it's a user thread , It's going to stop in an endless loop . This is the difference between a daemon thread and a non daemon thread .
Application scenarios
Through the example code above , I believe you have understood the difference between daemons and ordinary threads , that , Let's talk about why we need daemons , And when to use , What is its application scenario ?
above , We already know that , If JVM There is no running non - daemonic thread in , This is the time ,JVM Will quit . let me put it another way , The daemons have the feature of automatically ending their life cycle , The non - daemons do not have this feature .
JVM The garbage collection thread in is a typical Guardian thread , If we don't have this feature , What will happen ?
When JVM To exit , Because the garbage collection thread is still running , Causes the program to be unable to exit , This is awkward !!! thus it can be seen , The importance of daemons .
Generally speaking , Daemons are often used to perform background tasks , But what? , You want the program to exit , Or say JVM Exit time , Threads can automatically shut down , here , Daemons are your first choice .
边栏推荐
- Web性能测试工具之ab入门篇
- V831——AprilTag标签识别
- 渗透测试工具 - msf生成后门免杀
- 近期群友遇到的软件测试面试题分享
- Postgresql源码(8)Xlog初始化
- Given an integer array nums and a target value target, find the two integers whose sum is the target value in the array, and return their array subscripts.
- 平衡二叉树
- 【论文笔记】—特征可视化—ZFNet—2014-ECCV
- 一文教你学会怎么设计测试用例
- 【AUTOSAR-DEM】-2.4-APP SWC如何读取MIL灯状态以及配置MIL灯
猜你喜欢

多叉树--->B树和B+树

SAP Fiori Launchpad 上看不到任何 tile 应该怎么办?

Swd/jtag communication failure and no target connected

UOS安装MariaDB

I'm back

HCIA-R&S自用笔记(6)(网络层)ICMP、IP协议基础及分片

2022年P气瓶充装考试试题及模拟考试

(pc+wap) industry general website of Zhimeng template company

SWD/JTAG Communication Failure和No Target Connected

Tianyi beyond has a pre-sale of 157700 from the reform, and gross Innovation: the product power is absolutely in the first camp
随机推荐
【论文笔记】—特征可视化—ZFNet—2014-ECCV
Postgresql源码(9)Xlog注册
PostgreSQL source code (8) xlog initialization
[UVM] Axi VIP uses the register model to access registers, and the ID is fixed to 0
美国压力激增 TikTok更换全球安全主管
Multitree -- > b tree and b+ tree
作用域、构造器详解
"Wild pointer" and Dachang classic dynamic memory error written test questions
Dataset:White Wine Quality白葡萄酒品质数据集的简介、下载、使用方法之详细攻略
15. 三数之和【List<List<Integer>> ans、 ans.add(Arrays.asList(nums[i], nums[j], nums[k]))】
主进程 主线程和子线程 三者退出的关系
An excellent graphical tool for information collection maltego
一个元素位于另一个元素之上,点击上面的元素引发下面元素事件操作
slab为什么要进行染色处理
深度学习小记 - 正则化,优化器,线性回归,逻辑斯蒂回归
[paper notes] - face recognition facenet - 2015-cvpr
Stc8h development (XIV): I2C drive rx8025t high-precision real-time clock chip
w806开发板驱动ov2640读取jpeg图片1600x1200分辨率
Postgresql源码(10)Xlog组装
Which vite plug-ins can quickly provide development efficiency