RFR: 8301684: Fix test code to not get finalizer deprecation warnings

Afshin Zafari duke at openjdk.org
Fri Mar 10 08:31:19 UTC 2023


To replace the finalizer use in the code, the `Cleaner` approach is used as stated in [Oracle doc on deprecated finalize() method](https://docs.oracle.com/javase/10/docs/api/java/lang/Object.html#finalize()).
Briefly:
1.  An instance of `Cleaner` (`java.lang.ref`) is created.
2. Using the `Cleaner`, an object is registered with a `Runnable` callback that is notified when the object is no longer reachable (GC'ed).
3. Write code in the callback to do other cleanings.
4. Or, use the `Cleanable` object which is returned from registration above and call its `clean` method to explicitly clean the object.


Cleaner c = new Cleaner();
Cleanable cleanable = c.register(an_obj, a_runnable);
...
  //JVM notifies by calling the 
    a_runnable.run() {...}
... 
// possible to explicit cleaning 
cleanable.clean(an_obj);

-------------

Commit messages:
 - 8301684: Fix test code to not get finalizer deprecation warnings

Changes: https://git.openjdk.org/jdk/pull/12968/files
 Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=12968&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8301684
  Stats: 109 lines in 2 files changed: 61 ins; 41 del; 7 mod
  Patch: https://git.openjdk.org/jdk/pull/12968.diff
  Fetch: git fetch https://git.openjdk.org/jdk pull/12968/head:pull/12968

PR: https://git.openjdk.org/jdk/pull/12968


More information about the hotspot-runtime-dev mailing list