如何解决“不要覆盖Object.finalize()方法”的问题。问题 [英] How to address the "Do not override the Object.finalize() method" issue

查看:509
本文介绍了如何解决“不要覆盖Object.finalize()方法”的问题。问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我找到了一个工具[SonarQube]来帮助我发现代码中的潜在威胁,我已经解决了该工具报告的所有问题。

Recently I figure out one tool [SonarQube] who helps me to find out the potential threats into code, I have addressed all issues reported by the tool.

但是,这也给我带来了被保护的void finalize()方法的潜在威胁问题,该方法已被我覆盖,该工具向我显示一条消息请勿覆盖Object.finalize()方法。

But it also gives me a potential threats issue for "protected void finalize()" method which is been override by me, tool displaying me a message "Do not override the Object.finalize() method".

任何人都可以帮我解决这个问题的方法,重写的方法还包括一些业务逻辑。

Can anybody please help me how to address this issue, the overrided method also include some business logic.

推荐答案

从不。决不! 从不将任何业务逻辑放入 finalize()中。覆盖 finalize()的唯一情况是当对象分配一些资源时,您却忘记了释放它们。在那种情况下,您的finalize()可以检查是否仍在分配资源,迫使它们释放,并在日志中发出警告,或者类似的事情。但是在任何正常情况下,您都不需要覆盖它。
另外,您不能保证,在最后一刻会调用finalize。因此,您可能有未完成的对象保留在堆中,直到世界末日为止,反之亦然,一旦丢失引用,您的对象就可以完成。另外,不能保证对象将按哪个顺序完成。我想,无论您的业务逻辑包含什么,那都不是您想要的。

Never. Never! Never put any business logic in finalize(). The only case to override finalize() is when your object allocates some resources, that you forget to free. In that case your finalize() could check is resources still allocated, force them to free, and warn about it in log, or something like that. But in any normal circumstances you do not need to override it. Also, you are not guaranteed, in exactly moment finalize will be invoked. So, you may have unfinalized object remains in the heap until end of the world, or, vice versa, your object could be finalized as soon as reference is lost. In additional, it is not guarantee, in which order objects will be finalized. Whatever your business logic contains, I suppose, that is not what you want.

UPD 另外:不能保证最终完成()(在大多数情况下,保证不会)在与您的应用程序相同的线程中执行,因此,在其中存在未同步的代码可能会导致无法预测的结果,同步的代码以及大型业务逻辑也可能减慢执行速度

UPD Also: it is not guaranteed that finalize() will (in most cases it is guaranteed that not) be executed in the same thread as your application, so, having unsynchronized code there could make unpredictable results, synchronized code, also large business logic could slow down an execution of the GC-thread, and therefore slow down entire application.

而不是覆盖 finalize(),而是实现一些显式方法(例如 close() done()),在完成此对象后将显式调用它们。
另外,在资源有限的设备上进行开发时,优良作法是通过多次调用init / done方法来重用对象,从而可以重用内部结构,而无需不必要的垃圾收集。

Instead of overriding finalize(), implement some explicit methods (e.g. close(), or done()), which you will explicitly call when done with this object. Also, when developing under limited-resource devices, it is good practice to reuse objects by multiple calling init/done methods, which allows to reuse internal structure, without unnecessary garbage collection.

因此,总而言之,您的帮助工具说的没错:覆盖 finalize()时-您做错了事。

So, in conclusion, your helping tool said that right: when overriding finalize() - you are doing something wrong.

这篇关于如何解决“不要覆盖Object.finalize()方法”的问题。问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆