java DestroyJavaVM 线程总是在运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34433267/
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-11-02 22:54:29  来源:igfitidea点击:

DestroyJavaVM thread ALWAYS running

javamultithreadingjvmjava-threads

提问by KidCrippler

When profiling my application I came across a weird behavior - the DestroyJavaVM thread is ALWAYS running - 100% of the time.

在分析我的应用程序时,我遇到了一个奇怪的行为——DestroyJavaVM 线程总是在运行——100% 的时间。

enter image description hereAfter doing a little research on the subject, on which there's hardly any valuable information online, all I understood is that this thread is supposed to unload the JVM upon exit.

在此处输入图片说明在对该主题进行了一些研究后,网上几乎没有任何有价值的信息,我所理解的是该线程应该在 exit 时卸载 JVM

If that's the case, why is this thread in RUNNING state 100% of the time from the very first moment I start my application? Doesn't it consume valuable resources and therefore may cause an OutOfMemoryError(like I sometimes get)?

如果是这种情况,为什么从我启动应用程序的第一刻起该线程就 100% 处于 RUNNING 状态?它不会消耗宝贵的资源,因此可能会导致OutOfMemoryError(就像我有时会得到的那样)?

Is there any official reference to what this thread actually does and what triggers its initialization?

是否有任何官方参考该线程实际执行的操作以及触发其初始化的原因?

Thanks

谢谢

回答by OldCurmudgeon

This happens because most applications are run in threads.

发生这种情况是因为大多数应用程序都在线程中运行。

All POJOapps start by invoking the mainmethod. In it's most simple case this method will do all of the work, creating objects, calling methods etc. Once maincompletes, the JVM is told to shut down using a DestroyJavaVMthread which waits for all non-daemon threads to complete before doing it's work. This is to ensure that any non-daemon threads you create run to completion before the JVM is torn down.

所有POJO应用程序都从调用该main方法开始。在最简单的情况下,此方法将完成所有工作,创建对象,调用方法等。一旦main完成,JVM 会被告知使用一个DestroyJavaVM线程关闭,该线程在执行工作之前等待所有非守护线程完成。这是为了确保您创建的任何非守护线程在 JVM 被拆除之前运行完成。

An app with a GUI, however, normally runs as a number of threads. One for watching for system events such as keyboard or mouse events. One for maintaining the windows and display etc. The mainmethod of this kind of app will probably just start up all the required threads and exit. It still creates the DestroyJavaVMthread but now all that does is wait for all of your created threads to finish before tearing down the VM.

但是,具有 GUI 的应用程序通常作为多个线程运行。一种用于监视系统事件,例如键盘或鼠标事件。一个用于维护窗口和显示等main。这种应用程序的方法可能只是启动所有需要的线程并退出。它仍然创建DestroyJavaVM线程,但现在所做的只是等待所有创建的线程完成,然后再拆除 VM。

As a result, any app that creates threads and relies solely on their functionality will allways have a DestroyJavaVMthread waiting for it to finish. Since all it is doing is joining all other running threads it does not consume any resources.

因此,任何创建线程并仅依赖其功能的应用程序都会有一个DestroyJavaVM线程等待它完成。由于它所做的只是join运行所有其他正在运行的线程,因此它不会消耗任何资源。