/*
* 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
*
* <p>
* 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反射地来访问他
*
* <p>
* 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
*
* <p>
* {@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<Object> startFinalizer(Class<?> finalizableReferenceClass, Object frq) {
/*
* We use FinalizableReference.class for two things: 1) To invoke FinalizableReference.finalizeReferent() 2) To
* detect when FinalizableReference's class loader has to be garbage collected, at which point, Finalizer can
* stop running
*
* 我们使用FinalizableReference.class做两件事情
* 1) 调用FinalizableReference.finalizeReferent()
* 2) 检测FinalizableReference的class loader是否会被垃圾回收,此时Finalizer将会停止运行
*/
if (!finalizableReferenceClass.getName().equals(FINALIZABLE_REFERENCE)) {
throw new IllegalArgumentException("Expected " + FINALIZABLE_REFERENCE + ".");
} // 启动一个Finalizer后台线程
Finalizer finalizer = new Finalizer(finalizableReferenceClass, frq);
Thread thread = new Thread(finalizer);
thread.setName(Finalizer.class.getName());
thread.setDaemon(true); try {
if (inheritableThreadLocals != null) { // 将thread的inheritableThreadLocals设为null
inheritableThreadLocals.set(thread, null);
}
} catch (Throwable t) {
logger.log(Level.INFO, "Failed to clear thread local values inherited" + " by reference finalizer thread.",
t);
} thread.start();
return finalizer.queue;
} /** FinalizableReference的class的弱引用 */
private final WeakReference<Class<?>> finalizableReferenceClassReference;
/** FinalizableReferenceQueue的虚引用 */
private final PhantomReference<Object> frqReference;
/** ReferenceQueue */
private final ReferenceQueue<Object> queue = new ReferenceQueue<Object>(); private static final Field inheritableThreadLocals = getInheritableThreadLocalsField(); /** Constructs a new finalizer thread. */
private Finalizer(Class<?> finalizableReferenceClass, Object frq) {
this.finalizableReferenceClassReference = new WeakReference<Class<?>>(finalizableReferenceClass); // Keep track of the FRQ that started us so we know when to stop.
this.frqReference = new PhantomReference<Object>(frq, queue);
} /**
* Loops continuously, pulling references off the queue and cleaning them up.
*
* 不断循环并将queue里的reference出队做清理
*/
@SuppressWarnings("InfiniteLoopStatement")
@Override
public void run() {
try {
while (true) {
try {
// remove()方法是一个死循环方法,直到返回Reference才会退出循环
cleanUp(queue.remove());
} catch (InterruptedException e) { /* ignore */
}
}
} catch (ShutDown shutDown) { /* ignore */
}
} /**
* Cleans up a single reference. Catches and logs all throwables.
*
* 清理一个reference
*/
private void cleanUp(Reference<?> reference) throws ShutDown {
// 获取 finalizeReferent() 方法
Method finalizeReferentMethod = getFinalizeReferentMethod();
do {
/*
* This is for the benefit of phantom references. Weak and soft references will have already been cleared by
* this point.
*
* 给虚引用使用的方法,如果是弱引用和软引用此时已经被cleared了
*/
// 将Reference内的referent清除
reference.clear(); if (reference == frqReference) {
/*
* The client no longer has a reference to the FinalizableReferenceQueue. We can stop.
*
* 当FinalizableReferenceQueue的reference自己被加入到了ReferenceQueue中时,停止Finalizer
*/
throw new ShutDown();
} try {
// 调用终结referent的方法
finalizeReferentMethod.invoke(reference);
} catch (Throwable t) {
logger.log(Level.SEVERE, "Error cleaning up after reference.", t);
} /*
* Loop as long as we have references available so as not to waste CPU looking up the Method over and over
* again.
*
* 只要queue里还有reference就持续循环
*/
// poll()是一个无延迟方法,如果queue里没有对象,则直接返回null,这点与remove()不同
} while ((reference = queue.poll()) != null);
} /**
* Looks up FinalizableReference.finalizeReferent() method.
*/
private Method getFinalizeReferentMethod() throws ShutDown {
// 通过class的引用获取class
Class<?> finalizableReferenceClass = finalizableReferenceClassReference.get();
if (finalizableReferenceClass == null) {
/*
* FinalizableReference's class loader was reclaimed. While there's a chance that other finalizable
* references could be enqueued subsequently (at which point the class loader would be resurrected by virtue
* of us having a strong reference to it), we should pretty much just shut down and make sure we don't keep
* it alive any longer than necessary.
*/
throw new ShutDown();
}
try {
return finalizableReferenceClass.getMethod("finalizeReferent");
} catch (NoSuchMethodException e) {
throw new AssertionError(e);
}
} public static Field getInheritableThreadLocalsField() {
try {
Field inheritableThreadLocals = Thread.class.getDeclaredField("inheritableThreadLocals");
inheritableThreadLocals.setAccessible(true);
return inheritableThreadLocals;
} catch (Throwable t) {
logger.log(Level.INFO, "Couldn't access Thread.inheritableThreadLocals."
+ " Reference finalizer threads will inherit thread local" + " values.");
return null;
}
} /** Indicates that it's time to shut down the Finalizer. */
@SuppressWarnings("serial")
// Never serialized or thrown out of this class.
private static class ShutDown extends Exception {
}
}

Guava Finalizer的更多相关文章

  1. Spring cache简单使用guava cache

    Spring cache简单使用 前言 spring有一套和各种缓存的集成方式.类似于sl4j,你可以选择log框架实现,也一样可以实现缓存实现,比如ehcache,guava cache. [TOC ...

  2. Guava库介绍之实用工具类

    作者:Jack47 转载请保留作者和原文出处 欢迎关注我的微信公众账号程序员杰克,两边的文章会同步,也可以添加我的RSS订阅源. 本文是我写的Google开源的Java编程库Guava系列之一,主要介 ...

  3. Google Java编程库Guava介绍

    本系列想介绍下Java下开源的优秀编程库--Guava[ˈgwɑːvə].它包含了Google在Java项目中使用一些核心库,包含集合(Collections),缓存(Caching),并发编程库(C ...

  4. [Java 缓存] Java Cache之 Guava Cache的简单应用.

    前言 今天第一次使用MarkDown的形式发博客. 准备记录一下自己对Guava Cache的认识及项目中的实际使用经验. 一: 什么是Guava Guava工程包含了若干被Google的 Java项 ...

  5. [转载]Google Guava官方教程(中文版)

      原文链接  译文链接 译者: 沈义扬,罗立树,何一昕,武祖  校对:方腾飞 引言 Guava工程包含了若干被Google的 Java项目广泛依赖 的核心库,例如:集合 [collections] ...

  6. java开发人员,最应该学习和熟练使用的工具类。google guava.(谷歌 瓜娃)

    学习参考文章: http://blog.csdn.net/wisgood/article/details/13297535 http://ifeve.com/google-guava/ http:// ...

  7. Guava学习笔记(一)概览

    Guava是谷歌开源的一套Java开发类库,以简洁的编程风格著称,提供了很多实用的工具类, 在之前的工作中应用过Collections API和Guava提供的Cache,不过对Guava没有一个系统 ...

  8. Guava monitor

    Guava的com.google.util.concurrent类库提供了相对于jdk java.util.concurrent包更加方便实用的并发类,Monitor类就是其中一个.Monitor类在 ...

  9. 使用Guava EventBus构建publish/subscribe系统

    Google的Guava类库提供了EventBus,用于提供一套组件内publish/subscribe的解决方案.事件总线EventBus,用于管理事件的注册和分发.在系统中,Subscribers ...

随机推荐

  1. 017.Zabbix宏介绍

    一 宏介绍 Zabbix宏的作用是便于在模板.Items.Trigger中的引用,名称为{$名称},宏的字符范围为:A~Z/0~9/_. 如:net.tcp.service[ssh,{$SSH_POR ...

  2. 美团开源Graver框架:用“雕刻”诠释iOS端UI界面的高效渲染

    Graver 是一款高效的 UI 渲染框架,它以更低的资源消耗来构建十分流畅的 UI 界面.Graver 独创性的采用了基于绘制的视觉元素分解方式来构建界面,得益于此,该框架能让 UI 渲染过程变得更 ...

  3. iOS技术篇:sizeToFit 和 sizeThatFits 区别

    sizeToFit:会计算出最优的 size 而且会改变自己的size UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , , ...

  4. 手把手教你做爬虫---基于NodeJs

    前言: 趁着北京今儿天气格外的蓝,我觉得我得干点什么,于是乎,卷起袖子,整理一下最近做爬虫的那些事儿. 目标:爬取北京大学软件与微电子学院的所有新闻,并将内容及图片存储到本地. 设计思路:经过对北京大 ...

  5. UOJ.52.[UR #4]元旦激光炮(交互 思路)

    题目链接 \(Description\) 交互库中有三个排好序的,长度分别为\(n_a,n_b,n_c\)的数组\(a,b,c\).你需要求出所有元素中第\(k\)小的数.你可以调用至多\(100\) ...

  6. node-webkit开发桌面应用

    Node-Webkit能够做什么呢?(打开链接看discuss) github 项目源:https://github.com/rogerwang 导言 node-webkit 是一个很神奇的桌面客户端 ...

  7. BZOJ4115 : [Wf2015]Tile Cutting

    设一种方案里三角形上三个点的坐标分别为$(0,0),(-a,b),(c,d)$,则得到的平行四边形的面积为$ac+bd$. 设$d(n)$为$n$的约数个数,$D$为$d$的生成函数,则答案的生成函数 ...

  8. hdu 5773 The All-purpose Zero 线段树 dp

    The All-purpose Zero 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5773 Description ?? gets an seq ...

  9. SecureCRT发送心跳机制保持SSH在线(解决阿里云ECS)

    设置如下:

  10. bash编程之xargs实用技巧

    xargs结合管道操作符|,可以完成很多看似复杂的问题: 1.快速删除所有.log日志文件 机器运行久了,就会有各式各样的日志文件,散落在各个目录下,可以利用下面的方法: find ./ -name ...