Reference源码分析 首先我们先看一下Reference类的注释: /** * Abstract base class for reference objects. This class defines the * operations common to all reference objects. Because reference objects are * implemented in close cooperation with the garbage collector, th…
Overview The java.lang.ref package provides more flexible types of references than are otherwise available, permitting limited interaction between the application and the Java Virtual Machine (JVM) garbage collector. It is an important package, centr…
定义 ReferenceQueue是引用队列,用于存放待回收的引用对象. 说明 对于软引用.弱引用和虚引用,如果我们希望当一个对象被垃圾回收器回收时能得到通知,进行额外的处理,这时候就需要使用到引用队列了. 在一个对象被垃圾回收器扫描到将要进行回收时,其相应的引用包装类,即reference对象会被放入其注册的引用队列queue中.可以从queue中获取到相应的对象信息,同时进行额外的处理.比如反向操作,数据清理,资源释放等. 使用例子 public class ReferenceQueueTe…
在阅读Thinking in Java的Containers in depth一章中的Holding references时,提到了一个工具包java.lang.ref,说这是个为Java垃圾回收提供了很大的灵活性的包. 并引出了抽象类Reference还有它的三个子类,书上看了好几次都一脸懵逼……最后百度了很久现在简单记录总结一下. 一.为什么要有这个Reference类呢? 一般的对象在程序中,都是可获取的,也就是有直接引用的,或者用更专业的词(等等会介绍)就是强引用的对象.一般对于这样可以…