gpt4 book ai didi

java - Object.finalize() 是做什么的?

转载 作者:搜寻专家 更新时间:2023-10-31 20:34:44 25 4
gpt4 key购买 nike

finalize() 是 java.lang.Object 类中的 9 个方法之一。函数状态的 api 文档:

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

  • 但是 finalize() 方法实际上做了什么?
  • 如果我们手动调用 finalize() 方法会怎样?

最佳答案

默认情况下它什么都不做,但它 gives you an interception point如果你想执行一些操作

这是java.lang.Objectfinalize()的实现

  protected void finalize() throws Throwable { }

The general contract of finalize is that it is invoked if and when the JavaTM virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. The finalize method may take any action, including making this object available again to other threads; the usual purpose of finalize, however, is to perform cleanup actions before the object is irrevocably discarded. For example, the finalize method for an object that represents an input/output connection might perform explicit I/O transactions to break the connection before the object is permanently discarded.


另见

关于java - Object.finalize() 是做什么的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19645371/

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