守护进程什么时候有用?(When are daemon threads useful?)

编程入门 行业动态 更新时间:2024-10-25 23:23:20
守护进程什么时候有用?(When are daemon threads useful?)

我知道Deamon主持后台线程。 我们可以通过调用setDaemon(true)来创建我们自己的守护进程线程。

我的问题是:为什么和什么时候需要创建我们的线程作为守护进程线程?

I know that Deamon threads background threads. We can create our own daemon thread by calling setDaemon(true).

My question is: why and when do we need to create our thread as daemon thread?

最满意答案

当所有运行的线程都是守护进程线程时,JVM退出。 所以想象你正在写一个简单的游戏,你的主要方法循环,​​直到你决定退出。 并且想象一下,在游戏开始的时候,你会开始一个线程,不断地轮询一些网站来触发警报。 当您决定结束游戏时,您希望JVM退出。 你不希望无尽的轮询阻止游戏结束。 所以你使这个轮询线程成为一个守护进程线程。

The JVM exits when all the running threads are daemon threads. So imagine you're writing a simple game where your main method loops until you decide to quit. And imagine that at the start of the game, you start a thread that will endlessly poll some website to trigger alerts. You would like the JVM to exit when you decide to end the game. You don't want the endless polling to prevent the game from ending. So you make this polling thread a daemon thread.

更多推荐

本文发布于:2023-07-31 02:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340443.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:什么时候   进程   有用   threads   daemon

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!