java - Linux 关机和 Java 关机 Hook

标签 java linux signals shutdown

当我在后台运行 Java 进程并关闭计算机 (ArchLinux) 时,计算机会等待几秒钟以终止我在 Java 中的关闭 Hook 吗?

最佳答案

如果你想在系统关闭时做一些事情,你需要添加一个关闭 Hook (假设你已经这样做了):

Runtime.getRuntime().addShutdownHook(
new Thread(new Runnable() {
    @Override
    public void run() {
        // This method will be executed and Virtual Machine will close when this method will return
    }
}));

如果花费太多时间,系统将永远等待完全终止

来自文档 Runtime#addShutdownHook

When the virtual machine is terminated due to user logoff or system shutdown the underlying operating system may only allow a fixed amount of time in which to shut down and exit. It is therefore inadvisable to attempt any user interaction or to perform a long-running computation in a shutdown hook.

关于java - Linux 关机和 Java 关机 Hook ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41210466/

相关文章:

java - LeanFT 项目中的 Jar 文件 - 类 Not Acceptable

java - 程序仅读取.txt文件java中的最后一行

java - 检查 FTP 服务器上的文件是否存在

linux - 在 fd 上实现的计时器

java - 如何在 Java 守护进程中捕获 linux 用户注销信号?

java - 如何在类中引用 EntityPlayer 方法

c++ - Iperf 2.x 客户端如何检测它发送的流量?

linux - awk从列中找到最大值和最小值并存储在其他列中

C - 强制终止时刷新文件缓冲区

perl - 获取在 Perl 中发送信号的 TTY