Package uk.ac.starlink.util
Class Cleaner
java.lang.Object
uk.ac.starlink.util.Cleaner
Manages a set of object references and corresponding cleaning actions.
It is used to manage end-of-life actions for an object,
in particular to replace use of finalizers, which at Java 21 are
deprecated for removal (and which are generally frowned upon).
This is a more-or-less drop-in replacement for the
java.lang.ref.Cleaner
class introduced in Java 9.
If the target platform for this package gets upgraded to Java 9 or greater,
this class can be retired in favour of the JRE implementation.
The getInstance()
method lazily constructs a singleton instance,
which is probably suitable for most purposes. But you can construct
your own if you want.
- Since:
- 25 Jul 2024
- Author:
- Mark Taylor
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
Cleanable represents an object and a cleaning action registered in a Cleaner. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Cleaner
Returns a lazily constructed singleton instance of this class.Registers an object and a cleaning action to run when the object becomes phantom reachable.
-
Constructor Details
-
Cleaner
public Cleaner()Constructor.
-
-
Method Details
-
register
Registers an object and a cleaning action to run when the object becomes phantom reachable.- Parameters:
obj
- the object to monitoraction
- a Runnable to invoke when the object becomes phantom reachable- Returns:
- a Cleanable instance
-
getInstance
Returns a lazily constructed singleton instance of this class.- Returns:
- singleton instance
-