<tfoot id='hJer9'></tfoot>

      <i id='hJer9'><tr id='hJer9'><dt id='hJer9'><q id='hJer9'><span id='hJer9'><b id='hJer9'><form id='hJer9'><ins id='hJer9'></ins><ul id='hJer9'></ul><sub id='hJer9'></sub></form><legend id='hJer9'></legend><bdo id='hJer9'><pre id='hJer9'><center id='hJer9'></center></pre></bdo></b><th id='hJer9'></th></span></q></dt></tr></i><div id='hJer9'><tfoot id='hJer9'></tfoot><dl id='hJer9'><fieldset id='hJer9'></fieldset></dl></div>
    1. <legend id='hJer9'><style id='hJer9'><dir id='hJer9'><q id='hJer9'></q></dir></style></legend>
    2. <small id='hJer9'></small><noframes id='hJer9'>

        <bdo id='hJer9'></bdo><ul id='hJer9'></ul>
      1. 在 Java 中替换 finalize()

        Replacing finalize() in Java(在 Java 中替换 finalize())
          <tbody id='Z3DwS'></tbody>
      2. <tfoot id='Z3DwS'></tfoot>
      3. <legend id='Z3DwS'><style id='Z3DwS'><dir id='Z3DwS'><q id='Z3DwS'></q></dir></style></legend>

          <small id='Z3DwS'></small><noframes id='Z3DwS'>

            • <i id='Z3DwS'><tr id='Z3DwS'><dt id='Z3DwS'><q id='Z3DwS'><span id='Z3DwS'><b id='Z3DwS'><form id='Z3DwS'><ins id='Z3DwS'></ins><ul id='Z3DwS'></ul><sub id='Z3DwS'></sub></form><legend id='Z3DwS'></legend><bdo id='Z3DwS'><pre id='Z3DwS'><center id='Z3DwS'></center></pre></bdo></b><th id='Z3DwS'></th></span></q></dt></tr></i><div id='Z3DwS'><tfoot id='Z3DwS'></tfoot><dl id='Z3DwS'><fieldset id='Z3DwS'></fieldset></dl></div>
                  <bdo id='Z3DwS'></bdo><ul id='Z3DwS'></ul>
                  本文介绍了在 Java 中替换 finalize()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  Object.finalize() 在 Java 9 中已被弃用,我想我明白其中的原因,但我很难看到如何替换它.

                  Object.finalize() is deprecated in Java 9, and I think I understand the reasons why, but I'm having trouble seeing how to replace it.

                  我有一个名为 Configuration 的实用程序类,它本质上只有一个实例,它拥有应用程序中的所有内容并在应用程序期间持续存在.它提供的服务之一是记录:在第一次请求记录消息时,会创建一个记录器(由于各种遗留原因,它是我自己的记录器而不是标准记录器),并在配置对象的字段中保存一个引用,并且在应用程序终止时,无论是正常还是异常,我都想释放记录器持有的任何资源(这是一个黑匣子,因为我的库的用户可以提供他们自己的实现).

                  I have a utility class called Configuration which essentially has a single instance that owns everything in the application and lasts for the duration of the application. One of the services it provides is logging: on first request to log a message, a logger is created (for various legacy reasons it's my own Logger rather than a standard one), with a reference held in a field of the Configuration object, and on application termination, whether normal or abnormal, I want to release any resources held by the logger (which is a black box since users of my library can supply their own implementation).

                  目前这是通过调用 logger.close()Configuration.finalize() 方法实现的.

                  Currently this is achieved with a Configuration.finalize() method that calls logger.close().

                  我应该怎么做?

                  推荐答案

                  Java 9 引入了 Cleaner 和 Cleanable 实用程序类,它们负责将幻像引用连接到队列和清空该队列的清理线程.

                  Java 9 introduces the Cleaner and Cleanable utility classes which take care of wiring up phantom references to a queue and a cleaning thread draining that queue.

                  虽然这可以让您分离出将在拥有对象死亡后执行事后清理的见证,但所有关于 GC 触发的资源管理的警告仍然适用,即仍然最好依赖 AutoClosable 和 try-with-resources 块来管理资源的生命周期,而不是在垃圾收集器上.

                  While this lets you separate out the witness that will perform a post-mortem cleanup after the owning object has died, all the caveats about GC-triggered resource management still apply, i.e. it is still preferable to rely on AutoClosable and try-with-resources blocks to manage the lifecycle of resources, not on the garbage collector.

                  这篇关于在 Java 中替换 finalize()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Maven JAVA_HOME environment variable is not defined correctly, but it is(Maven JAVA_HOME 环境变量未正确定义,但它是)
                  Java System Environment Variable(Java 系统环境变量)
                  Java -classpath option(Java -classpath 选项)
                  Read environment variable in SpringBoot(在 SpringBoot 中读取环境变量)
                  Tomcat 8 - context.xml use Environment Variable in Datasource(Tomcat 8 - context.xml 在数据源中使用环境变量)
                  Issue with JAVA_HOME(JAVA_HOME 的问题)
                    <bdo id='rOhpu'></bdo><ul id='rOhpu'></ul>
                        <tbody id='rOhpu'></tbody>
                      • <i id='rOhpu'><tr id='rOhpu'><dt id='rOhpu'><q id='rOhpu'><span id='rOhpu'><b id='rOhpu'><form id='rOhpu'><ins id='rOhpu'></ins><ul id='rOhpu'></ul><sub id='rOhpu'></sub></form><legend id='rOhpu'></legend><bdo id='rOhpu'><pre id='rOhpu'><center id='rOhpu'></center></pre></bdo></b><th id='rOhpu'></th></span></q></dt></tr></i><div id='rOhpu'><tfoot id='rOhpu'></tfoot><dl id='rOhpu'><fieldset id='rOhpu'></fieldset></dl></div>
                        <tfoot id='rOhpu'></tfoot>

                          <small id='rOhpu'></small><noframes id='rOhpu'>

                          <legend id='rOhpu'><style id='rOhpu'><dir id='rOhpu'><q id='rOhpu'></q></dir></style></legend>