Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
有什么方法可以在实际关闭之前收到有关 JVM 关闭或 System.exit 调用的通知?我想要这个,所以我的应用程序将能够干净地退出。我意识到这样的事情不太可能存在,但是,也许还有这样的事情?
看看Runtime.addShutdownHook
从 API 文档:
Java 虚拟机关闭以响应两种事件:
使用Runtime.addShutdownHook您可以在这些情况下运行代码。
Runtime.addShutdownHook
Runtime.getRuntime().addShutdownHook()您可能想探索一些方法。
Runtime.getRuntime().addShutdownHook()