Class Finalizer
- java.lang.Object
-
- com.google.common.base.internal.Finalizer
-
- All Implemented Interfaces:
java.lang.Runnable
public class Finalizer extends java.lang.Object implements java.lang.RunnableThread that finalizes referents. All references should implementcom.google.common.base.FinalizableReference.While this class is public, we consider it to be *internal* and not part of our published API. It is public so we can access it reflectively across class loaders in secure environments.
This class can't depend on other Guava code. If we were to load this class in the same class loader as the rest of Guava, this thread would keep an indirect strong reference to the class loader and prevent it from being garbage collected. This poses a problem for environments where you want to throw away the class loader. For example, dynamically reloading a web application or unloading an OSGi bundle.
com.google.common.base.FinalizableReferenceQueueloads this class in its own class loader. That way, this class doesn't prevent the main class loader from getting garbage collected, and this class can detect when the main class loader has been garbage collected and stop itself.
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.reflect.Constructor<java.lang.Thread>bigThreadConstructorprivate static java.lang.StringFINALIZABLE_REFERENCEName of FinalizableReference.class.private java.lang.ref.WeakReference<java.lang.Class<?>>finalizableReferenceClassReferenceprivate java.lang.ref.PhantomReference<java.lang.Object>frqReferenceprivate static java.lang.reflect.FieldinheritableThreadLocalsprivate static java.util.logging.Loggerloggerprivate java.lang.ref.ReferenceQueue<java.lang.Object>queue
-
Constructor Summary
Constructors Modifier Constructor Description privateFinalizer(java.lang.Class<?> finalizableReferenceClass, java.lang.ref.ReferenceQueue<java.lang.Object> queue, java.lang.ref.PhantomReference<java.lang.Object> frqReference)Constructs a new finalizer thread.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private booleancleanUp(java.lang.ref.Reference<?> reference)Cleans up a single reference.private static java.lang.reflect.Constructor<java.lang.Thread>getBigThreadConstructor()private java.lang.reflect.MethodgetFinalizeReferentMethod()Looks up FinalizableReference.finalizeReferent() method.private static java.lang.reflect.FieldgetInheritableThreadLocalsField()voidrun()Loops continuously, pulling references off the queue and cleaning them up.static voidstartFinalizer(java.lang.Class<?> finalizableReferenceClass, java.lang.ref.ReferenceQueue<java.lang.Object> queue, java.lang.ref.PhantomReference<java.lang.Object> frqReference)Starts the Finalizer thread.
-
-
-
Field Detail
-
logger
private static final java.util.logging.Logger logger
-
FINALIZABLE_REFERENCE
private static final java.lang.String FINALIZABLE_REFERENCE
Name of FinalizableReference.class.- See Also:
- Constant Field Values
-
finalizableReferenceClassReference
private final java.lang.ref.WeakReference<java.lang.Class<?>> finalizableReferenceClassReference
-
frqReference
private final java.lang.ref.PhantomReference<java.lang.Object> frqReference
-
queue
private final java.lang.ref.ReferenceQueue<java.lang.Object> queue
-
bigThreadConstructor
private static final java.lang.reflect.Constructor<java.lang.Thread> bigThreadConstructor
-
inheritableThreadLocals
private static final java.lang.reflect.Field inheritableThreadLocals
-
-
Method Detail
-
startFinalizer
public static void startFinalizer(java.lang.Class<?> finalizableReferenceClass, java.lang.ref.ReferenceQueue<java.lang.Object> queue, java.lang.ref.PhantomReference<java.lang.Object> frqReference)Starts the Finalizer thread. FinalizableReferenceQueue calls this method reflectively.- Parameters:
finalizableReferenceClass- FinalizableReference.class.queue- a reference queue that the thread will poll.frqReference- a phantom reference to the FinalizableReferenceQueue, which will be queued either when the FinalizableReferenceQueue is no longer referenced anywhere, or when its close() method is called.
-
run
public void run()
Loops continuously, pulling references off the queue and cleaning them up.- Specified by:
runin interfacejava.lang.Runnable
-
cleanUp
private boolean cleanUp(java.lang.ref.Reference<?> reference)
Cleans up a single reference. Catches and logs all throwables.- Returns:
- true if the caller should continue, false if the associated FinalizableReferenceQueue is no longer referenced.
-
getFinalizeReferentMethod
private java.lang.reflect.Method getFinalizeReferentMethod()
Looks up FinalizableReference.finalizeReferent() method.
-
getInheritableThreadLocalsField
private static java.lang.reflect.Field getInheritableThreadLocalsField()
-
getBigThreadConstructor
private static java.lang.reflect.Constructor<java.lang.Thread> getBigThreadConstructor()
-
-