Cleaner
manages a set of object references and corresponding cleaning actions.
Cleaning actions are registered
to run after the cleaner is notified that the object has become
phantom reachable.
The cleaner uses PhantomReference
and ReferenceQueue
to be
notified when the reachability
changes.
Each cleaner operates independently, managing the pending cleaning actions
and handling threading and termination when the cleaner is no longer in use.
Registering an object reference and corresponding cleaning action returns
a Cleanable
. The most efficient use is to explicitly invoke
the clean
method when the object is closed or
no longer needed.
The cleaning action is a Runnable
to be invoked at most once when
the object has become phantom reachable unless it has already been explicitly cleaned.
Note that the cleaning action must not refer to the object being registered.
If so, the object will not become phantom reachable and the cleaning action
will not be invoked automatically.
The execution of the cleaning action is performed by a thread associated with the cleaner. All exceptions thrown by the cleaning action are ignored. The cleaner and other cleaning actions are not affected by exceptions in a cleaning action. The thread runs until all registered cleaning actions have completed and the cleaner itself is reclaimed by the garbage collector.
The behavior of cleaners during System.
is implementation specific. No guarantees are made relating
to whether cleaning actions are invoked or not.
Unless otherwise noted, passing a null
argument to a constructor or
method in this class will cause a
NullPointerException
to be thrown.
API Note
The cleaning action is invoked only after the associated object becomes phantom reachable, so it is important that the object implementing the cleaning action does not hold references to the object. In this example, a static class encapsulates the cleaning state and action. An "inner" class, anonymous or not, must not be used because it implicitly contains a reference to the outer instance, preventing it from becoming phantom reachable. The choice of a new cleaner or sharing an existing cleaner is determined by the use case.
If the CleaningExample is used in a try-finally block then the
close
method calls the cleaning action.
If the close
method is not called, the cleaning action is called
by the Cleaner when the CleaningExample instance has become phantom reachable.
public class CleaningExample implements AutoCloseable {
// A cleaner (preferably one shared within a library,
// but for the sake of example, a new one is created here)
private static final Cleaner cleaner = Cleaner.create();
// State class captures information necessary for cleanup.
// It must hold no reference to the instance being cleaned
// and therefore it is a static inner class in this example.
static class State implements Runnable {
State(...) {
// initialize State needed for cleaning action
}
public void run() {
// cleanup action accessing State, executed at most once
}
}
private final State state;
private final Cleaner.Cleanable cleanable;
public CleaningExample() {
this.state = new State(...);
this.cleanable = cleaner.register(this, state);
}
public void close() {
cleanable.clean();
}
}
The cleaning action could be a lambda but all too easily will capture
the object reference, by referring to fields of the object being cleaned,
preventing the object from becoming phantom reachable.
Using a static nested class, as above, will avoid accidentally retaining the
object reference.
Cleaning actions should be prepared to be invoked concurrently with other cleaning actions. Typically the cleaning actions should be very quick to execute and not block. If the cleaning action blocks, it may delay processing other cleaning actions registered to the same cleaner. All cleaning actions registered to a cleaner should be mutually compatible.
Modifier and Type | Class and Description |
---|---|
public static interface | Cleaner.
|
Modifier and Type | Field and Description |
---|---|
pack-priv final CleanerImpl | impl
The Cleaner implementation. |
Access | Constructor and Description |
---|---|
private |
Modifier and Type | Method and Description |
---|---|
public static Cleaner | |
public static Cleaner | Returns: a newCleaner a threadFactory)ThreadFactory to return a new Thread
to process cleaning actionsReturns a new |
public Cleaner. |
impl | back to summary |
---|---|
pack-priv final CleanerImpl impl The Cleaner implementation. |
Cleaner | back to summary |
---|---|
private Cleaner() Construct a Cleaner implementation and start it. |
create | back to summary |
---|---|
public static Cleaner create() Returns a new
The cleaner creates a The cleaner terminates when it is phantom reachable and all of the registered cleaning actions are complete.
|
create | back to summary |
---|---|
public static Cleaner create(ThreadFactory threadFactory) Returns a new
A thread from the thread factory's The cleaner terminates when it is phantom reachable and all of the registered cleaning actions are complete.
|
register | back to summary |
---|---|
public Cleaner. Registers an object and a cleaning action to run when the object becomes phantom reachable. Refer to the API Note above for cautions about the behavior of cleaning actions. The given object is kept strongly reachable (and therefore not eligible for cleaning) during the register() method. Memory consistency effects:
Actions in a thread prior to calling |
Cleanable
represents an object and a
cleaning action registered in a Cleaner
.
Modifier and Type | Method and Description |
---|---|
public void |
clean | back to summary |
---|---|
public void clean() Unregisters the cleanable and invokes the cleaning action.
The cleanable's cleaning action is invoked at most once
regardless of the number of calls to |