diff options
Diffstat (limited to 'guava/src/com/google/common/base/FinalizableReferenceQueue.java')
-rw-r--r-- | guava/src/com/google/common/base/FinalizableReferenceQueue.java | 41 |
1 files changed, 8 insertions, 33 deletions
diff --git a/guava/src/com/google/common/base/FinalizableReferenceQueue.java b/guava/src/com/google/common/base/FinalizableReferenceQueue.java index 2ca3681..f300b33 100644 --- a/guava/src/com/google/common/base/FinalizableReferenceQueue.java +++ b/guava/src/com/google/common/base/FinalizableReferenceQueue.java @@ -16,12 +16,8 @@ package com.google.common.base; -import com.google.common.annotations.VisibleForTesting; -import java.io.Closeable; - import java.io.FileNotFoundException; import java.io.IOException; -import java.lang.ref.PhantomReference; import java.lang.ref.Reference; import java.lang.ref.ReferenceQueue; import java.lang.reflect.Method; @@ -41,7 +37,7 @@ import java.util.logging.Logger; * @author Bob Lee * @since 2.0 (imported from Google Collections Library) */ -public class FinalizableReferenceQueue implements Closeable { +public class FinalizableReferenceQueue { /* * The Finalizer thread keeps a phantom reference to this object. When the client (for example, a * map built by MapMaker) no longer has a strong reference to this object, the garbage collector @@ -95,8 +91,6 @@ public class FinalizableReferenceQueue implements Closeable { */ final ReferenceQueue<Object> queue; - final PhantomReference<Object> frqRef; - /** * Whether or not the background thread started successfully. */ @@ -108,28 +102,24 @@ public class FinalizableReferenceQueue implements Closeable { @SuppressWarnings("unchecked") public FinalizableReferenceQueue() { // We could start the finalizer lazily, but I'd rather it blow up early. - queue = new ReferenceQueue<Object>(); - frqRef = new PhantomReference<Object>(this, queue); + ReferenceQueue<Object> queue; boolean threadStarted = false; try { - startFinalizer.invoke(null, FinalizableReference.class, queue, frqRef); + queue = (ReferenceQueue<Object>) + startFinalizer.invoke(null, FinalizableReference.class, this); threadStarted = true; } catch (IllegalAccessException impossible) { throw new AssertionError(impossible); // startFinalizer() is public } catch (Throwable t) { logger.log(Level.INFO, "Failed to start reference finalizer thread." + " Reference cleanup will only occur when new references are created.", t); + queue = new ReferenceQueue<Object>(); } + this.queue = queue; this.threadStarted = threadStarted; } - @Override - public void close() { - frqRef.enqueue(); - cleanUp(); - } - /** * Repeatedly dequeues references from the queue and invokes {@link * FinalizableReference#finalizeReferent()} on them until the queue is empty. This method is a @@ -189,16 +179,8 @@ public class FinalizableReferenceQueue implements Closeable { * we needn't create a separate loader. */ static class SystemLoader implements FinalizerLoader { - // This is used by the ClassLoader-leak test in FinalizableReferenceQueueTest to disable - // finding Finalizer on the system class path even if it is there. - @VisibleForTesting - static boolean disabled; - @Override public Class<?> loadFinalizer() { - if (disabled) { - return null; - } ClassLoader systemLoader; try { systemLoader = ClassLoader.getSystemClassLoader(); @@ -272,10 +254,7 @@ public class FinalizableReferenceQueue implements Closeable { /** Creates a class loader with the given base URL as its classpath. */ URLClassLoader newLoader(URL base) { - // We use the bootstrap class loader as the parent because Finalizer by design uses - // only standard Java classes. That also means that FinalizableReferenceQueueTest - // doesn't pick up the wrong version of the Finalizer class. - return new URLClassLoader(new URL[] {base}, null); + return new URLClassLoader(new URL[] {base}); } } @@ -299,11 +278,7 @@ public class FinalizableReferenceQueue implements Closeable { */ static Method getStartFinalizer(Class<?> finalizer) { try { - return finalizer.getMethod( - "startFinalizer", - Class.class, - ReferenceQueue.class, - PhantomReference.class); + return finalizer.getMethod("startFinalizer", Class.class, Object.class); } catch (NoSuchMethodException e) { throw new AssertionError(e); } |