# Class Cleaner

java.lang.Object
uk.ac.starlink.util.Cleaner

public class Cleaner extends Object
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 Classes
Modifier and Type
Class
Description
`static interface`
`Cleaner.Cleanable`
Cleanable represents an object and a cleaning action registered in a Cleaner.

## Constructor Summary

Constructors
Constructor
Description
`Cleaner()`
Constructor.

## Method Summary

Modifier and Type
Method
Description
`static Cleaner`
`getInstance()`
Returns a lazily constructed singleton instance of this class.
`Cleaner.Cleanable`
`register(Object obj, Runnable action)`
Registers an object and a cleaning action to run when the object becomes phantom reachable.

### Methods inherited from class Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### Cleaner

public Cleaner()
Constructor.

## Method Details

### register

public Cleaner.Cleanable register(Object obj, Runnable action)
Registers an object and a cleaning action to run when the object becomes phantom reachable.

Parameters:
  `obj` - the object to monitor
  `action` - a Runnable to invoke when the object becomes phantom reachable
Returns:
  a Cleanable instance

### getInstance

public static Cleaner getInstance()
Returns a lazily constructed singleton instance of this class.

Returns:
  singleton instance