转换用户线程的使用->守护线程

发布于 2024-10-16 00:06:34 字数 221 浏览 4 评论 0原文

我读到,守护线程是由 JVM 使用和控制的。 JVM 创建它们并负责它们的终止。用户线程由用户控制。 也有人说,我们可以通过调用 setDaemon() 方法将用户线程转换为守护线程。

但是,这种转换有什么用?一旦用户线程成为守护线程,JVM 是否会控制用户线程?

如果我错过了什么,请告诉我。

谢谢。

I read, daemon threads are used and controlled by JVM. JVM creates them and also looks after their termination.User threads are controlled by user.
It is also said, we can convert a user thread to daemon thread by calling setDaemon() method.

But, what is the use of such conversion ? Does JVM takes the control of user thread once it has become a dameon thread ?

Let me know if I missed something.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戏剧牡丹亭 2024-10-23 00:06:34

我相信用户和守护线程始终处于 JVM 的控制之下。 (如果不是这样,谁来负责?)

这是区别(来自 http://www.xyzws.com/javafaq/what-is-difference- Between-user-and-daemon-thread-in-java/196< /a>):

这两种类型的区别
线程的数量很简单:如果
Java 运行时确定唯一的
应用程序中运行的线程是
守护线程(即,没有
用户线程存在)Java
运行时立即关闭
应用程序,有效地停止所有
守护线程停滞不前。
为了使应用程序
继续运行,它必须始终有
至少一个实时用户线程。总共
Java 运行时处理的其他方面
守护线程和用户线程
完全相同的方式。

I believe that user and daemon threads are always under the JVM's control. (If that wasn't the case, who would be in charge?)

Here's the distinction (from http://www.xyzws.com/javafaq/what-is-difference-between-user-and-daemon-thread-in-java/196):

The difference between these two types
of threads is straightforward: If the
Java runtime determines that the only
threads running in an application are
daemon threads (i.e., there are no
user threads in existence) the Java
runtime promptly closes down the
application, effectively stopping all
daemon threads dead in their tracks.
In order for an application to
continue running, it must always have
at least one live user thread. In all
other respects the Java runtime treats
daemon threads and user threads in
exactly the same manner.

拍不死你 2024-10-23 00:06:34

守护线程不会阻止应用程序在仍在工作时关闭。它们更适合在应用程序处于活动状态时需要完成的任务,但在其他情况下可以安全地终止。

Daemon threads don't prevent the application from shutting down while they're still doing work. They're more for tasks that need to be done while the app is alive, but are safe to kill off otherwise.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文