java 守护线程什么时候有用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7067578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:31:32  来源:igfitidea点击:

When are daemon threads useful?

java

提问by user414967

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

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

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

我的问题是:为什么以及何时需要将我们的线程创建为守护线程?

回答by JB Nizet

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.

当所有正在运行的线程都是守护线程时,JVM 退出。因此,想象一下您正在编写一个简单的游戏,其中您的 main 方法循环直到您决定退出。想象一下,在游戏开始时,您启动了一个线程,该线程将无休止地轮询某个网站以触发警报。当您决定结束游戏时,您希望 JVM 退出。您不希望无休止的投票阻止游戏结束。所以你让这个轮询线程成为守护线程。

回答by Snps

A Deamon thread is automatically terminated by the JVM when all "normal" threads are terminated. Normal threads are never automatically terminated.

当所有“正常”线程终止时,JVM 会自动终止守护线程。普通线程永远不会自动终止。

回答by panzerschreck

Services that you wish to offer to your consumers without any user-interation by way of essentially user-threads form the primary use-case for setting a user thread as a daemon.

您希望通过本质上的用户线程向您的消费者提供的服务,而无需任何用户交互,这构成了将用户线程设置为守护进程的主要用例。

As a consequence, until user-threads exist JVM gurantees that daemon threads run continously. You can find examples like GC, UI Thread etc.. those are daemons.

因此,直到用户线程存在 JVM 才能保证守护线程连续运行。您可以找到 GC、UI 线程等示例。这些都是守护进程。

Hope it helps.

希望能帮助到你。

回答by M Platvoet

As other have pointed, a daemon thread does not prevent the JVM from exiting when the program finishes when this thread is still running.

正如其他人指出的那样,守护线程不会阻止 JVM 在该线程仍在运行时程序完成时退出。

In general you'd rather not create daemon threads, unless you are absolutely certain the thread has no side effects. Since you can't tell when the thread stops, finalizer blocks are not run, nor are any stack unwound. So try avoiding using IO operations in daemon threads because it can corrupt data.

一般来说,您宁愿不创建守护线程,除非您绝对确定该线程没有副作用。由于您无法判断线程何时停止,因此不会运行终结器块,也不会展开任何堆栈。所以尽量避免在守护线程中使用 IO 操作,因为它会破坏数据。

回答by AlexR

Normally program terminates when all its threads exited their run()method. Daemon threads do not prevent program to terminate even if they are still running, i.e. executing run().

通常,当所有线程退出它们的run()方法时,程序就会终止。守护线程不会阻止程序终止,即使它们仍在运行,即执行run().

So, you should use daemon thread if you wish not to prevent program termination when the thread is still running. It is typical for example for long-time periodic tasks but actually depend very much on your program, your design and your taste.

因此,如果您不希望在线程仍在运行时阻止程序终止,则应使用守护线程。例如,它是典型的长期周期性任务,但实际上在很大程度上取决于您的程序、您的设计和您的品味。

回答by Val

I used them with Timer to delete files that cannot be deleted immediately. That is, I generate .exe files, run and then delete them. But there is 50% chance that executable.deletefails, seemingly because image is still blocked by the process in termination. You can reliably delete executable image only after process has finished completely. But, you never know how long it takes. You set .deleteOnExit therefore instead of .delete. But, you do not want to wait until java machine terminates also. It can take very long and you do not want millions of useless stupid .exe files, that you do not need anymore, hanging in the file system. You therefore schedule executable.delete in the timer to happen one-two seconds later. The timer however cannot be usual thread. If it is so, it will block your program from terminating even if there are no files to delete. I can easily make it daemon however because whether my files are deleted or not by timer is immaterial -- the files will be removed either way: either by daemon or java exit. I think it is perfect use of daemon.

我将它们与 Timer 一起使用来删除无法立即删除的文件。也就是说,我生成.exe文件,运行然后删除它们。但有 50% 的可能性executable.delete失败,似乎是因为图像在终止时仍被进程阻塞。只有在进程完全完成后,您才能可靠地删除可执行映像。但是,你永远不知道需要多长时间。因此,您设置 .deleteOnExit 而不是 .delete。但是,您也不想等到 java 机器终止。这可能需要很长时间,而且您不希望数百万个您不再需要的无用的愚蠢 .exe 文件挂在文件系统中。因此,您将计时器中的 executable.delete 安排在一两秒后发生。然而计时器不能是普通线程。如果是这样,即使没有要删除的文件,它也会阻止您的程序终止。但是,我可以轻松地将其设为守护程序,因为我的文件是否被计时器删除并不重要——文件将通过两种方式删除:通过守护程序或 java 退出。

回答by Shivendra Prakash Shukla

Daemon threads in Java are like a service providers for other threads or objects running in the same process as the daemon thread. Daemon threads are used for background supporting tasks and are only needed while normal threads are executing. If normal threads are not running and remaining threads are daemon threads then the interpreter exits.

Java 中的守护线程就像是其他线程或对象的服务提供者,这些线程或对象与守护线程运行在同一进程中。守护线程用于后台支持任务,仅在正常线程执行时需要。如果正常线程没有运行并且剩余线程是守护线程,则解释器退出。

When a new thread is created it inherits the daemon status of its parent. Normal thread and daemon threads differ in what happens when they exit. When the JVM halts any remaining daemon threads are abandoned: finally blocks are not executed, stacks are not unwound – JVM just exits. Due to this reason daemon threads should be used sparingly and it is dangerous to use them for tasks that might perform any sort of I/O.

创建新线程时,它会继承其父线程的守护进程状态。普通线程和守护线程在退出时会发生什么不同。当 JVM 停止时,任何剩余的守护线程都会被放弃:finally 块不会被执行,堆栈不会被解开——JVM 只是退出。由于这个原因,应该谨慎使用守护线程,将它们用于可能执行任何类型 I/O 的任务是危险的。