java.lang.ref.Reference

Java代码 复制代码 收藏代码
  1. Reference提供了一些引用的基本方法以及静态代码块最高优先级启动ReferenceHandler线程
  1. Reference提供了一些引用的基本方法以及静态代码块最高优先级启动ReferenceHandler线程
Reference提供了一些引用的基本方法以及静态代码块最高优先级启动ReferenceHandler线程
Java代码 复制代码 收藏代码
  1. package xxx;
  2. /**
  3. * Abstract base class for reference objects. This class defines the
  4. * operations common to all reference objects. Because reference objects are
  5. * implemented in close cooperation with the garbage collector, this class may
  6. * not be subclassed directly.
  7. * 引用对象的抽象基类。这个类定义了所有引用对象的通用行为。
  8. * 因为引用对象是通过与垃圾回收期密切合作来实现的,所以不能直接为此类创建子类.
  9. *
  10. *
  11. * @version 1.43, 04/10/06
  12. * @author Mark Reinhold
  13. * @since 1.2
  14. */
  15. public abstract class Reference<T> {
  16. /* A Reference instance is in one of four possible internal states:
  17. * 一种引用实例是可能是四种内部状态之一:
  18. * Active: Subject to special treatment by the garbage collector. Some
  19. * time after the collector detects that the reachability of the
  20. * referent has changed to the appropriate state, it changes the
  21. * instance's state to either Pending or Inactive, depending upon
  22. * whether or not the instance was registered with a queue when it was
  23. * created. In the former case it also adds the instance to the
  24. * pending-Reference list. Newly-created instances are Active.
  25. * 激活:垃圾回收器特别处理的主题。有时候在回收器检测到被引用(对象)的可达性被改变成适当
  26. * 的状态,它会把实例的状态改变成等待状态或者未激活状态,这取决于实例是否被一个队列注册当
  27. * 它被创建。在前一种情况下(等待状态),它也往等待-引用集合增加实例。
  28. *
  29. *
  30. * Pending: An element of the pending-Reference list, waiting to be
  31. * enqueued by the Reference-handler thread. Unregistered instances
  32. * are never in this state.
  33. * 等待:一个等待-引用集合里的元素,等待被引用处理线程放入队列中。
  34. * 未注册的实例永远不会在这个状态
  35. *
  36. * Enqueued: An element of the queue with which the instance was
  37. * registered when it was created. When an instance is removed from
  38. * its ReferenceQueue, it is made Inactive. Unregistered instances are
  39. * never in this state.
  40. * 入队:实例被创建的时候被登记注册成一个队列的元素。当一个实例从引用队列中删除,它变成非激活状态。
  41. * 未注册的实例永远不会在这个状态。
  42. *
  43. * Inactive: Nothing more to do. Once an instance becomes Inactive its
  44. * state will never change again.
  45. * 非激活:不会再做什么。一旦一个实例成为非激活的,它的状态永远不会被改变。
  46. *
  47. *
  48. * The state is encoded in the queue and next fields as follows:
  49. * 状态在队列里被处理并且每个状态所表现的属性如下:
  50. *
  51. *
  52. * Active: queue = ReferenceQueue with which instance is registered, or
  53. * ReferenceQueue.NULL if it was not registered with a queue; next =
  54. * null.
  55. * 激活:queue=引用队列时候,实例被它注册,
  56. * 或者实例不被注册,当queue=ReferenceQueue.NULL时候;
  57. * next=null.
  58. *
  59. * Pending: queue = ReferenceQueue with which instance is registered;
  60. * next = Following instance in queue, or this if at end of list.
  61. * 等待:queue=引用队列时候,实例被它注册,
  62. * next=剩下的queue队列里面的实例,或者=this,如果this是队列的最后一个。
  63. *
  64. * Enqueued: queue = ReferenceQueue.ENQUEUED; next = Following instance
  65. * in queue, or this if at end of list.
  66. * 入队:queue=ReferenceQueue.ENQUEUED
  67. * next=剩下的queue队列里面的实例,或者=this,如果this是队列的最后一个
  68. *
  69. *
  70. * Inactive: queue = ReferenceQueue.NULL; next = this.
  71. * 终止:队列=ReferenceQueue.NULL next=this
  72. *
  73. * With this scheme the collector need only examine the next field in order
  74. * to determine whether a Reference instance requires special treatment: If
  75. * the next field is null then the instance is active; if it is non-null,
  76. * then the collector should treat the instance normally.
  77. *
  78. *
  79. *
  80. * To ensure that concurrent collector can discover active Reference
  81. * objects without interfering with application threads that may apply
  82. * the enqueue() method to those objects, collectors should link
  83. * discovered objects through the discovered field.
  84. */
  85. private T referent; /* Treated specially by GC */ //被GC引用的对象
  86. ReferenceQueue<? super T> queue;//引用队列
  87. Reference next;//下个引用
  88. transient private Reference<T> discovered; /* used by VM *///被VM引用的瞬态对象
  89. /* Object used to synchronize with the garbage collector. The collector
  90. * must acquire this lock at the beginning of each collection cycle. It is
  91. * therefore critical that any code holding this lock complete as quickly
  92. * as possible, allocate no new objects, and avoid calling user code.
  93. * GC线程在回收的时候的锁
  94. * 对象被用来和GC同步。GC必须获得锁在每个回收的生命周期。
  95. * 更关键的是任何持有锁的代码尽可能快的执行完,没有分配新的对象,并避免使用使用者的代码。
  96. *
  97. */
  98. static private class Lock { };
  99. private static Lock lock = new Lock();
  100. /* List of References waiting to be enqueued. The collector adds
  101. * References to this list, while the Reference-handler thread removes
  102. * them. This list is protected by the above lock object.
  103. * 排队的引用集合。当处理引用的线程删除引用时候,收集器添加引用到这个集合。
  104. * 这个集合受上面的对象锁保护。
  105. */
  106. private static Reference pending = null;
  107. /* High-priority thread to enqueue pending References
  108. * 处理排队等待的引用的高优先的线程
  109. */
  110. private static class ReferenceHandler extends Thread {
  111. ReferenceHandler(ThreadGroup g, String name) {
  112. super(g, name);
  113. }
  114. public void run() {
  115. for (;;) {
  116. /*
  117. * 给pending赋值
  118. * 如果pending.next=pending,pending=null;否则pending=pengding.next,最后把pending.next=pending
  119. * 下次执行线程里的代码时候pending=null了,再下次执行同步代码块就线程阻塞了
  120. *
  121. * 如果pending属性为空,释放锁的对象监视器,阻塞当前线程
  122. * */
  123. Reference r;
  124. synchronized (lock) {
  125. if (pending != null) {
  126. r = pending;
  127. Reference rn = r.next;
  128. pending = (rn == r) ? null : rn;
  129. r.next = r;
  130. } else {
  131. try {
  132. lock.wait();
  133. } catch (InterruptedException x) { }
  134. continue;
  135. }
  136. }
  137. // Fast path for cleaners
  138. if (r instanceof Cleaner) {
  139. ((Cleaner)r).clean();
  140. continue;
  141. }
  142. /*
  143. * ReferenceQueue.NULL的实现:
  144. * private static class Null extends ReferenceQueue {
  145. * boolean enqueue(Reference r) {
  146. * return false;
  147. }
  148. }
  149. * 如果Q不为空,把引用放入Queue
  150. * 在刚创建一个引用,第二个参数没放Queue时候,为空。
  151. * */
  152. ReferenceQueue q = r.queue;
  153. if (q != ReferenceQueue.NULL) q.enqueue(r);
  154. }
  155. }
  156. }
  157. static {
  158. //取得当前线程组
  159. ThreadGroup tg = Thread.currentThread().getThreadGroup();
  160. //取得最上层的System线程组
  161. for (ThreadGroup tgn = tg;
  162. tgn != null;
  163. tg = tgn, tgn = tg.getParent());
  164. //创建线程对象
  165. Thread handler = new ReferenceHandler(tg, "Reference Handler");
  166. /* If there were a special system-only priority greater than
  167. * MAX_PRIORITY, it would be used here
  168. */
  169. handler.setPriority(Thread.MAX_PRIORITY);//设置最高优先级
  170. handler.setDaemon(true);//标记守护线程或用户线程
  171. handler.start();//守护线程启动
  172. }
  173. /* -- Referent accessor and setters -- */
  174. /**
  175. * Returns this reference object's referent. If this reference object has
  176. * been cleared, either by the program or by the garbage collector, then
  177. * this method returns <code>null</code>.
  178. *
  179. * @return The object to which this reference refers, or
  180. * <code>null</code> if this reference object has been cleared
  181. * 获得引用对象
  182. */
  183. public T get() {
  184. return this.referent;
  185. }
  186. /**
  187. * Clears this reference object. Invoking this method will not cause this
  188. * object to be enqueued.
  189. *
  190. * <p> This method is invoked only by Java code; when the garbage collector
  191. * clears references it does so directly, without invoking this method.
  192. * 清除引用对象
  193. */
  194. public void clear() {
  195. this.referent = null;
  196. }
  197. /* -- Queue operations -- */
  198. /**
  199. * Tells whether or not this reference object has been enqueued, either by
  200. * the program or by the garbage collector. If this reference object was
  201. * not registered with a queue when it was created, then this method will
  202. * always return <code>false</code>.
  203. *
  204. * @return <code>true</code> if and only if this reference object has
  205. * been enqueued
  206. */
  207. public boolean isEnqueued() {
  208. /* In terms of the internal states, this predicate actually tests
  209. whether the instance is either Pending or Enqueued
  210. 是否处于等待状态
  211. 判断条件:队列不为空,
  212. 它的next属性不为空。
  213. 刚初始化一个Reference时候,next属性肯定是空的,因此肯定不处于等待状态
  214. */
  215. synchronized (this) {
  216. return (this.queue != ReferenceQueue.NULL) && (this.next != null);
  217. }
  218. }
  219. /**
  220. * Adds this reference object to the queue with which it is registered,
  221. * if any.
  222. *
  223. * <p> This method is invoked only by Java code; when the garbage collector
  224. * enqueues references it does so directly, without invoking this method.
  225. *
  226. * @return <code>true</code> if this reference object was successfully
  227. * enqueued; <code>false</code> if it was already enqueued or if
  228. * it was not registered with a queue when it was created
  229. * 加入等待队列
  230. */
  231. public boolean enqueue() {
  232. return this.queue.enqueue(this);
  233. }
  234. /* -- Constructors -- */
  235. Reference(T referent) {
  236. this(referent, null);
  237. }
  238. Reference(T referent, ReferenceQueue<? super T> queue) {
  239. this.referent = referent;
  240. this.queue = (queue == null) ? ReferenceQueue.NULL : queue;
  241. }
  242. }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值