/* * Copyright (C) 2008 The Guava Authors Licensed under the Apache License, Version 2.0 (the "License"); you may not use * this file except in compliance with the License. You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or implied. See the License for the specific language governing permissions and limitations under the * License. */package com.google.common.base.internal;import java.lang.ref.PhantomReference;import java.lang.ref.Reference;import java.lang.ref.ReferenceQueue;import java.lang.ref.WeakReference;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.util.logging.Level;import java.util.logging.Logger;/** * Thread that finalizes referents. All references should implement { @code com.google.common.base.FinalizableReference}. * * 用来清理被引用对象.所有的引用必须实现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. * * 当这个类是public时,我们将其作为internal而非published的API.因为他是public的所以我们可以在一个安全的环境中通过class loaders反射地来访问他 * *
* This class can't depend on other Google Collections code. If we were to load this class in the same class loader as * the rest of Google Collections, 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. * * 这个类不能依赖于其他Google Collections代码.假如我们像其他Google Collections那样在同一个class loader中载入这个类,这个线程会保留一个 * 间接的强引用到class loader上并阻止他被垃圾回收.这会在当你想丢弃这个class loader的时候造成一个问题.例如,动态的重新载入一个web应用程序或者 * 卸载一个osgi bundle * *
* {
@code 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. * * FinalizableReferenceQueue使用它自己的class loader加载这个类,也就是说这个类不会阻止主class loader被垃圾回收,并且这个在检测到主 * class loader被垃圾回收后会停止他自己 */public class Finalizer implements Runnable { private static final Logger logger = Logger.getLogger(Finalizer.class.getName()); /** Name of FinalizableReference.class. */ private static final String FINALIZABLE_REFERENCE = "com.google.common.base.FinalizableReference"; /** * Starts the Finalizer thread. FinalizableReferenceQueue calls this method reflectively. * 启动Finalizer线程,FinalizableReferenceQueue使用反射调用这个方法 * * @param finalizableReferenceClass FinalizableReference.class * @param frq reference to instance of FinalizableReferenceQueue that started this thread * @return ReferenceQueue which Finalizer will poll */ public static ReferenceQueue