出错时退出 JVM

发布于 2024-12-10 07:32:53 字数 239 浏览 0 评论 0原文

我正在开发这个需要加载本机库的应用程序。启动本机库加载的类的加载在与主线程不同的线程上运行。 当发生错误(抛出错误)时,我希望虚拟机终止,但在这种情况下它不会。当然,当我无法加载本机库时,我不能继续,因为那是应用程序的核心。 我假设我的应用程序没有终止,因为主线程至少启动了 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 技术交流群。

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

发布评论

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

评论(1

半步萧音过轻尘 2024-12-17 07:32:53

为了正确回答您的问题,我需要了解有关您生成的其他线程的更多信息。可以将它们设置为守护程序类型线程吗?如果没有,您通常如何优雅地退出这些线程?

这是一种强力方法(在加载本机库的线程内):

try {
    methodToLoadLibrary();
    doWhatYouNeedToDo();
} finally {
    System.exit(1);
}

但这假设您将在该线程终止时退出整个进程。

如果您有一个可以调用的方法来使线程正常终止,则可以用以下内容替换 System.exit(1); 调用:

Collection<Stoppable> sList;
for(Stoppable s : sList) {
    s.stop();
}

其中 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):

try {
    methodToLoadLibrary();
    doWhatYouNeedToDo();
} finally {
    System.exit(1);
}

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:

Collection<Stoppable> sList;
for(Stoppable s : sList) {
    s.stop();
}

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.

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