简体   繁体   中英

What does it mean for an object to be “finalizer-reachable” in Java?

I am reading the Java 8 spec and I see this definition for finalizer-reachable:

A finalizer-reachable object can be reached from some finalizable object through some chain of references, but not from any live thread.

What would this look like in code? I don't have an intuition for what something like this would even look like.

In the example code below, when an instance of Example becomes unreachable, the object that os refers to will be finalizer-reachable.

  public class Example {

      private OutputStream os;

      public Example(OutputStream os) {
          this.os = 0s;
      }

      protected void finalize() {
          try {
              os.close();
          } catch (IOException ex) {
              // ignore it
          }
     }
  }

However, if the Example instance was no longer eligible for finalization (eg because it had been finalized previously and then "resurrected" during finalization), then os would not be finalizer-reachable.

The "finalizer-reachable" state is about specifying that objects that may be referred to during finalization don't get deleted prematurely. The specification does not state how this should be ensured. I imagine that it would not be possible for Java code (or even native code) to determine whether a specific object was is this state.

You have an object Foo. Foo is finalizable, and has no references in any variable in the program anymore. Foo has a member Bar. Bar references an object that is not referenced anywhere else. The object Bar references is therefor finalizer-reachable.

Basically its an object that cannot be reached through the code anymore, but can be reached through another object that cannot be reached through the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM