gpt4 book ai didi

java - 何时应该进行应用程序清理

转载 作者:行者123 更新时间:2023-12-01 12:36:59 24 4
gpt4 key购买 nike

我刚刚读到,我们可以使用关闭 Hook 作为一个例子,说明在应用程序退出之前提供清理函数是理想的选择。

我想知道的是,例如,如果您有一个像 Web 服务一样 24/7 运行的应用程序,那么什么时候是终止和清理执行程序服务等内容的理想时间。如果应用程序不应该关闭,那么我们真的没有方便的清理期或者确实没有干净关闭的能力吗?

谢谢

最佳答案

这个问题太宽泛,但如果我理解正确的话 Oracle docs解释一下:

A pool that is no longer referenced in a program AND has no remaining threads will be shutdown automatically. If you would like to ensure that unreferenced pools are reclaimed even if users forget to call shutdown(), then you must arrange that unused threads eventually die, by setting appropriate keep-alive times, using a lower bound of zero core threads and/or setting allowCoreThreadTimeOut(boolean).

例如,由 Executors.newSingleThreadExecutor() 创建的 ExecutorService 是 FinalizesDeleatedExecutorService 的实例。现在,ExecutorService 类具有 finalize() 方法,该方法调用包装的 ExecutorService 对象上的 shutdown() 方法。

关于java - 何时应该进行应用程序清理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25490891/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com