出错时退出 JVM
我正在开发这个需要加载本机库的应用程序。启动本机库加载的类的加载在与主线程不同的线程上运行。 当发生错误(抛出错误)时,我希望虚拟机终止,但在这种情况下它不会。当然,当我无法加载本机库时,我不能继续,因为那是应用程序的核心。 我假设我的应用程序没有终止,因为主线程至少启动了 3 个线程。连接线程 - 侦听小工具是否已连接,然后通知侦听器已连接或断开连接,以及其他资源管理线程。
当 jls 说应用程序不应尝试捕获错误时,我将如何结束我的应用程序?
I am working on this application that requires loading of native library. the loading of the class that initiates the loading of the native library is running on a different thread from the main thread.
When an error occurs (an error is thrown), i would expect that the vm terminates, but in this case it does not. certainly when i cant load the native library, i must not continue, because thats the core of the application.
I assume my application is not terminating because the main thread starts at least 3 threads. the connection thread-that listens if a gadget has been connected and then informs the listener that it has been connected or disconnected, and other resource management threads.
How would i end my application when the jls says that the application should not try to catch errors?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了正确回答您的问题,我需要了解有关您生成的其他线程的更多信息。可以将它们设置为守护程序类型线程吗?如果没有,您通常如何优雅地退出这些线程?
这是一种强力方法(在加载本机库的线程内):
但这假设您将在该线程终止时退出整个进程。
如果您有一个可以调用的方法来使线程正常终止,则可以用以下内容替换
System.exit(1);
调用:其中
Stoppable
是接口用于传达您希望这些线程正常关闭的信息。还有一些不太干净的方法,例如在其他线程上调用 Thread.stop 。
In order to answer your question properly, I'll need to know more information about the other threads that you spawned. Can they be set to daemon-type threads? If not, how do you normally gracefully exit those threads?
Here's a brute-force way (inside the thread that loads the native library):
but this assumes that you'll be exiting the entire process when this thread terminates.
If you have a method which can be called to make the threads terminate gracefully, you can replace the
System.exit(1);
call with this:where
Stoppable
is the interface for communicating that you want a graceful shutdown on those threads.There are also less-clean ways like calling
Thread.stop
on the other threads.