Class Finalizer
- All Implemented Interfaces:
Runnable
com.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.FinalizableReferenceQueue
loads 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
FieldsModifier and TypeFieldDescriptionprivate static final Constructor
<Thread> private static final String
Name of FinalizableReference.class.private final WeakReference
<Class<?>> private final PhantomReference
<Object> private static final Field
private static final Logger
private final ReferenceQueue
<Object> -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
Finalizer
(Class<?> finalizableReferenceClass, ReferenceQueue<Object> queue, PhantomReference<Object> frqReference) Constructs a new finalizer thread. -
Method Summary
Modifier and TypeMethodDescriptionprivate boolean
Cleans up the given reference and any other references already in the queue.private boolean
finalizeReference
(Reference<?> reference, Method finalizeReferentMethod) Cleans up the given reference.private static Constructor
<Thread> private Method
Looks up FinalizableReference.finalizeReferent() method.private static Field
void
run()
Loops continuously, pulling references off the queue and cleaning them up.static void
startFinalizer
(Class<?> finalizableReferenceClass, ReferenceQueue<Object> queue, PhantomReference<Object> frqReference) Starts the Finalizer thread.
-
Field Details
-
logger
-
FINALIZABLE_REFERENCE
Name of FinalizableReference.class.- See Also:
-
finalizableReferenceClassReference
-
frqReference
-
queue
-
bigThreadConstructor
-
inheritableThreadLocals
-
-
Constructor Details
-
Finalizer
private Finalizer(Class<?> finalizableReferenceClass, ReferenceQueue<Object> queue, PhantomReference<Object> frqReference) Constructs a new finalizer thread.
-
-
Method Details
-
startFinalizer
public static void startFinalizer(Class<?> finalizableReferenceClass, ReferenceQueue<Object> queue, PhantomReference<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. -
cleanUp
Cleans up the given reference and any other references already in the queue. Catches and logs all throwables.- Returns:
- true if the caller should continue to wait for more references to be added to the queue, false if the associated FinalizableReferenceQueue is no longer referenced.
-
finalizeReference
Cleans up the given reference. Catches and logs all throwables.- Returns:
- true if the caller should continue to clean up references from the queue, false if the associated FinalizableReferenceQueue is no longer referenced.
-
getFinalizeReferentMethod
Looks up FinalizableReference.finalizeReferent() method. -
getInheritableThreadLocalsField
-
getBigThreadConstructor
-