- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个关于 Runtime.getRuntime().addShutdownHook()
的问题.
假设我在 main 中启动了两个线程。两个线程都在处理一个集合,List<String> commonColection
.
当我按Ctrl+C
时我想保存这个 Collection 。我的问题是:线程是否在调用 ShutdownHooks 之前停止,或者我是否需要显式终止我的线程,然后保存我的集合?
最佳答案
When the virtual machine begins its shutdown sequence it will start all registered shutdown hooks in some unspecified order and let them run concurrently. When all the hooks have finished it will then run all uninvoked finalizers if finalization-on-exit has been enabled. Finally, the virtual machine will halt.
来自:Oracle Docu
当我读到这篇文章时,您将必须中止您的线程(不是Thread.abort
!而是使用某种方法优雅地结束它们!)并保存您的集合。至少您必须确保保存集合的正确状态。
如果线程不是“无限”并且没有守护进程,您可以让它们完成。但我不建议这样做,因为关闭 Hook 应该很快返回。
关于java - ShutdownHook何时被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14628672/
据我所知,System.exit 会触发关闭钩子(Hook)(如果注册了任何钩子(Hook)),因此关闭钩子(Hook)线程正在等待任何其他线程加入如果其他线程调用 System.exit() 我认为
我有一个应用程序,它的主要方法生成一百个线程(假设我们模拟一百个帐户)。我正在试验它,我希望它在被 Control-C 中断时只打印终止。 我读到您可以使用 ShutDownHooks 来做到这一点,
我发现要显示 JOptionPane 消息对话框,我需要 shutdownHooks 许可。它有什么作用?向第三方代码授予此权限可能存在哪些风险? 最佳答案 关闭 Hook 是虚拟机退出前(所有非守护
我遇到了这个异常: org.quartz.SchedulerException: SchedulerPlugin class not specified for plugin 'shutdownhoo
我需要从 ShutdownHook 调用该方法的非静态方法 removeLocksOnExit()。 该方法在 main 所在的同一个类中声明。 主要我有以下代码: //final Thread ma
假设 shutdownHook 为: class ShutdownHolder extends Thread { public void run() { Logger logg
我有一个 shutdownhook,它在进程终止时执行。然而,程序对 h2 数据库所做的更改不是持久的。只有在提交后让关闭线程等待一段时间,我才能在再次启动时看到数据库中的更改。 Runtime.ge
如果我有一个 Java 程序可以由于各种原因退出,例如: 因为设置为“关闭时退出”的主窗口已关闭 因为代码中有一些System.exit(0) 因为根本没有更多的窗口(并且没有设置为在关闭时退出)但是
这是我的主要方法,它包含一个 shutdownhook: public static void main(String args[]) { Runtime.getRuntime().addSh
我正在使用带有常规配置的 logback 1.1.10。我最近发现了shutdownHook preference但我还没有找到使用 groovy 配置启用它的方法。这可能吗?还是缺失的功能? 最佳答
有什么办法可以在关闭 Hook 中显示 joptionpanei.s 我需要在我的 shutdownhook 事件中显示一个确认对话框 最佳答案 如果有,它不会帮助你。 关闭 Hook 作为 JVM
我有以下 ShutdownHook 来检测应用程序何时退出: Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
我试图提供一个非常简单的“你想保存这个文档吗?”关闭我的应用程序时的对话框。 我使用了 WindowAdapter 来做到这一点。如果使用任何 GUI 操作关闭应用程序(单击 CLOSE 按钮、从任务
我们如何在 Web 应用程序中注册Shutdown hook? 在 web.xml 或 applicationContext.xml 中注册它有什么好处吗? 我知道如果我们使用带有主类的应用程序很简单
我正在使用 logback 中的元素; jmxConfigurator 声明如下: Thus, unless your application is a standalone Java applic
拥有 Spring Boot 应用程序,在调用 Shutdown hook @Predestroy 方法时,会调用应用程序关闭。但在打印日志后,控制台和文件、sys out 行中均不打印日志。看起来,
在下面的代码片段中,永远不会调用 finalize() 方法。但是当我在 addShutdownHook 中将 finalize() 替换为 close() 时。 finalize() 方法将被调用。
我正在 Tomcat 中使用 Jax-ws 发布 Web 服务 @WebService() public class ChatService ...followed by a constructor
这个问题在这里已经有了答案: 关闭 9 年前。 Possible Duplicate: When Shutdown Hooks Break Bad 想知道在JVM崩溃的场景下是否保证shutdown
我有一些 Java 代码来创建一个关闭钩子(Hook),以便在客户端按下 ctrl+C 时干净地退出: private static void shutdownHandler(Thread mainT
我是一名优秀的程序员,十分优秀!