有效 Java - 从不依赖终结器来更新关键持久状态
Effective Java - Never depend on a finalizer to update critical persistent state
为什么不应该这样做?由于执行垃圾回收时会调用终结器,为什么我们不能在这里添加持久化相关代码?
它说关键(!)持久数据。可以有终结器来写掉持久数据,但如果不写入这些数据就一定没问题。
因为丢失关键数据是不好的,所以它不应该进入终结器。
但是当 GC 必须收集所有内容时,怎么能不调用终结器呢?
好吧,一旦应用程序可以被强制终止,因此永远不会让 GC 有机会 运行.
正如书中所解释的那样
Not only does the JLS provide no guarantee that finalizers will get
executed promptly, it provides no guarantee that they'll get executed
at all. It is entirely possible, even likely, that a program
terminates without executing finalizers on some objects that are no
longer reachable.
为什么不应该这样做?由于执行垃圾回收时会调用终结器,为什么我们不能在这里添加持久化相关代码?
它说关键(!)持久数据。可以有终结器来写掉持久数据,但如果不写入这些数据就一定没问题。
因为丢失关键数据是不好的,所以它不应该进入终结器。
但是当 GC 必须收集所有内容时,怎么能不调用终结器呢? 好吧,一旦应用程序可以被强制终止,因此永远不会让 GC 有机会 运行.
正如书中所解释的那样
Not only does the JLS provide no guarantee that finalizers will get executed promptly, it provides no guarantee that they'll get executed at all. It is entirely possible, even likely, that a program terminates without executing finalizers on some objects that are no longer reachable.