public class

Cleaner

extends PhantomReference<T>
java.lang.Object
   ↳ java.lang.ref.Reference<T>
     ↳ java.lang.ref.PhantomReference<T>
       ↳ sun.misc.Cleaner

Class Overview

General-purpose phantom-reference-based cleaners.

Cleaners are a lightweight and more robust alternative to finalization. They are lightweight because they are not created by the VM and thus do not require a JNI upcall to be created, and because their cleanup code is invoked directly by the reference-handler thread rather than by the finalizer thread. They are more robust because they use phantom references, the weakest type of reference object, thereby avoiding the nasty ordering problems inherent to finalization.

A cleaner tracks a referent object and encapsulates a thunk of arbitrary cleanup code. Some time after the GC detects that a cleaner's referent has become phantom-reachable, the reference-handler thread will run the cleaner. Cleaners may also be invoked directly; they are thread safe and ensure that they run their thunks at most once.

Cleaners are not a replacement for finalization. They should be used only when the cleanup code is extremely simple and straightforward. Nontrivial cleaners are inadvisable since they risk blocking the reference-handler thread and delaying further cleanup and finalization.

Summary

Public Methods
void clean()
Runs this cleaner, if it has not been run before.
static Cleaner create(Object ob, Runnable thunk)
Creates a new cleaner.
[Expand]
Inherited Methods
From class java.lang.ref.PhantomReference
From class java.lang.ref.Reference
From class java.lang.Object

Public Methods

public void clean ()

Runs this cleaner, if it has not been run before.

public static Cleaner create (Object ob, Runnable thunk)

Creates a new cleaner.

Parameters
thunk The cleanup code to be run when the cleaner is invoked. The cleanup code is run directly from the reference-handler thread, so it should be as simple and straightforward as possible.
Returns
  • The new cleaner