有人能告诉我Java中的守护线程是什么吗?


当前回答

传统上,UNIX中的守护进程是那些不断在后台运行的进程,就像Windows中的服务一样。

Java中的守护线程是不阻止JVM退出的线程。特别是当只剩下守护线程时,JVM将退出。您可以通过调用线程上的setDaemon()方法来创建一个。

读取Daemon线程。

其他回答

在Java中,守护线程是一种不阻止Java虚拟机(JVM)退出的线程类型。 守护线程的主要目的是执行后台任务,特别是一些例行的周期性任务或工作。JVM退出时,守护进程线程也会死亡。

通过设置thread. setdaemon (true),线程变成守护线程。但是,您只能在线程启动之前设置这个值。

正如大家所解释的,守护线程不会限制JVM退出,所以基本上从退出的角度来看,它对应用程序来说是一个快乐的线程。

想要添加守护线程,当我提供一个API,比如将数据推送到第三方服务器/或JMS时,我可能需要在客户端JVM级别聚合数据,然后在一个单独的线程中发送到JMS。我可以使这个线程作为守护线程,如果这不是一个强制数据被推送到服务器。 这类数据类似于日志推送/聚合。

问候, Manish

Daemon thread is like daemon process which is responsible for managing resources,a daemon thread is created by the Java VM to serve the user threads. example updating system for unix,unix is daemon process. child of daemon thread is always daemon thread,so by default daemon is false.you can check thread as daemon or user by using "isDaemon()" method. so daemon thread or daemon process are basically responsible for managing resources. for example when you starting jvm there is garbage collector running that is daemon thread whose priority is 1 that is lowest,which is managing memory. jvm is alive as long as user thread is alive,u can not kill daemon thread.jvm is responsible to kill daemon threads.

守护进程: d(isk) a(nd) e(xecution) mon(itor) or or from de(vice) mon(itor)

Daemon(计算)的定义:

处理打印假脱机和文件传输等服务请求的后台进程,在不需要时处于休眠状态。

——来源:牛津词典英文版

Java中的守护线程是什么?

Daemon threads can shut down any time in between their flow, Non-Daemon i.e. user thread executes completely. Daemon threads are threads that run intermittently in the background as long as other non-daemon threads are running. When all of the non-daemon threads complete, daemon threads terminates automatically. Daemon threads are service providers for user threads running in the same process. The JVM does not care about daemon threads to complete when in Running state, not even finally block also let execute. JVM do give preference to non-daemon threads that is created by us. Daemon threads acts as services in Windows. The JVM stops the daemon threads when all user threads (in contrast to the daemon threads) are terminated. Hence daemon threads can be used to implement, for example, a monitoring functionality as the thread is stopped by the JVM as soon as all user threads have stopped.

答案已经有很多了;然而,也许我可以更清楚地解释这一点,因为当我阅读Daemon Threads时,最初,我有一种感觉,我很好地理解了它;然而,在玩了它并调试了一下之后,我发现了一个奇怪的行为。

我被教导:

如果我想让线程在主线程有序完成执行后立即死亡,我应该将其设置为Diamond。

我尝试了什么:

我从主线程中创建了两个线程,我只将其中一个设置为菱形; 在主线程有序完成执行后,那些新创建的线程都没有退出,但我预计,守护线程应该已经退出; 我浏览了许多博客和文章,到目前为止,我找到的最好、最清晰的定义来自《Java并发实践》一书,它非常清楚地指出:

7.4.2守护线程

Sometimes you want to create a thread that performs some helper function but you don’t want the existence of this thread to prevent the JVM from shutting down. This is what daemon threads are for. Threads are divided into two types: normal threads and daemon threads. When the JVM starts up, all the threads it creates (such as garbage collector and other housekeeping threads) are daemon threads, except the main thread. When a new thread is created, it inherits the daemon status of the thread that created it, so by default any threads created by the main thread are also normal threads. Normal threads and daemon threads differ only in what happens when they exit. When a thread exits, the JVM performs an inventory of running threads, and if the only threads that are left are daemon threads, it initiates an orderly shutdown. When the JVM halts, any remaining daemon threads are abandoned— finally blocks are not executed, stacks are not unwound—the JVM just exits. Daemon threads should be used sparingly—few processing activities can be safely abandoned at any time with no cleanup. In particular, it is dangerous to use daemon threads for tasks that might perform any sort of I/O. Daemon threads are best saved for “housekeeping” tasks, such as a background thread that periodically removes expired entries from an in-memory cache.