`
DarkMeteor
  • 浏览: 11754 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

java.lang.ref.Reference

阅读更多
Reference提供了一些引用的基本方法以及静态代码块最高优先级启动ReferenceHandler线程
package xxx;
 
/**
 * 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, this class may
 * not be subclassed directly.
 * 引用对象的抽象基类。这个类定义了所有引用对象的通用行为。
 * 因为引用对象是通过与垃圾回收期密切合作来实现的,所以不能直接为此类创建子类.
 * 
 *
 * @version  1.43, 04/10/06
 * @author   Mark Reinhold
 * @since    1.2
 */
public abstract class Reference<T> {
    /* A Reference instance is in one of four possible internal states:
     * 一种引用实例是可能是四种内部状态之一:
     *    Active: Subject to special treatment by the garbage collector.  Some
     *    time after the collector detects that the reachability of the
     *    referent has changed to the appropriate state, it changes the
     *    instance's state to either Pending or Inactive, depending upon
     *    whether or not the instance was registered with a queue when it was
     *    created.  In the former case it also adds the instance to the
     *    pending-Reference list.  Newly-created instances are Active.
     *	  激活:垃圾回收器特别处理的主题。有时候在回收器检测到被引用(对象)的可达性被改变成适当
     *	  的状态,它会把实例的状态改变成等待状态或者未激活状态,这取决于实例是否被一个队列注册当
     *    它被创建。在前一种情况下(等待状态),它也往等待-引用集合增加实例。
     *
     *
     *    Pending: An element of the pending-Reference list, waiting to be
     *    enqueued by the Reference-handler thread.  Unregistered instances
     *    are never in this state.
     *    等待:一个等待-引用集合里的元素,等待被引用处理线程放入队列中。
     *    未注册的实例永远不会在这个状态
     *
     *    Enqueued: An element of the queue with which the instance was
     *    registered when it was created.  When an instance is removed from
     *    its ReferenceQueue, it is made Inactive.  Unregistered instances are
     *    never in this state.
     *    入队:实例被创建的时候被登记注册成一个队列的元素。当一个实例从引用队列中删除,它变成非激活状态。
     *	  未注册的实例永远不会在这个状态。		
     *
     *    Inactive: Nothing more to do.  Once an instance becomes Inactive its
     *    state will never change again.
     *    非激活:不会再做什么。一旦一个实例成为非激活的,它的状态永远不会被改变。
     *    
     *
     * The state is encoded in the queue and next fields as follows:
     * 状态在队列里被处理并且每个状态所表现的属性如下:
     * 
     *
     *    Active: queue = ReferenceQueue with which instance is registered, or
     *    ReferenceQueue.NULL if it was not registered with a queue; next =
     *    null.
     *    激活:queue=引用队列时候,实例被它注册,
     *    或者实例不被注册,当queue=ReferenceQueue.NULL时候;
     *    next=null.
     *
     *    Pending: queue = ReferenceQueue with which instance is registered;
     *    next = Following instance in queue, or this if at end of list.
     *    等待:queue=引用队列时候,实例被它注册,
     *    next=剩下的queue队列里面的实例,或者=this,如果this是队列的最后一个。
     *
     *    Enqueued: queue = ReferenceQueue.ENQUEUED; next = Following instance
     *    in queue, or this if at end of list.
     *    入队:queue=ReferenceQueue.ENQUEUED 
     *    next=剩下的queue队列里面的实例,或者=this,如果this是队列的最后一个
     *    
     *    
     *    Inactive: queue = ReferenceQueue.NULL; next = this.
     *    终止:队列=ReferenceQueue.NULL next=this
     *
     * With this scheme the collector need only examine the next field in order
     * to determine whether a Reference instance requires special treatment: If
     * the next field is null then the instance is active; if it is non-null,
     * then the collector should treat the instance normally.
     * 
     * 
     * 
     * To ensure that concurrent collector can discover active Reference 
     * objects without interfering with application threads that may apply 
     * the enqueue() method to those objects, collectors should link 
     * discovered objects through the discovered field.
     */
    private T referent;  /* Treated specially by GC */ //被GC引用的对象
    ReferenceQueue<? super T> queue;//引用队列
    Reference next;//下个引用
    transient private Reference<T> discovered;  /* used by VM *///被VM引用的瞬态对象

    /* Object used to synchronize with the garbage collector.  The collector
     * must acquire this lock at the beginning of each collection cycle.  It is
     * therefore critical that any code holding this lock complete as quickly
     * as possible, allocate no new objects, and avoid calling user code.
     * GC线程在回收的时候的锁
     * 对象被用来和GC同步。GC必须获得锁在每个回收的生命周期。
     * 更关键的是任何持有锁的代码尽可能快的执行完,没有分配新的对象,并避免使用使用者的代码。
     * 
     */
    static private class Lock { };
    private static Lock lock = new Lock();

    /* List of References waiting to be enqueued.  The collector adds
     * References to this list, while the Reference-handler thread removes
     * them.  This list is protected by the above lock object.
     * 排队的引用集合。当处理引用的线程删除引用时候,收集器添加引用到这个集合。
     * 这个集合受上面的对象锁保护。
     */
    private static Reference pending = null;
    /* High-priority thread to enqueue pending References
     * 处理排队等待的引用的高优先的线程
     */
    private static class ReferenceHandler extends Thread {
    	ReferenceHandler(ThreadGroup g, String name) {
    		super(g, name);
    		}
    	public void run() {
    		for (;;) {
    			/*
    			 * 给pending赋值
    			 * 如果pending.next=pending,pending=null;否则pending=pengding.next,最后把pending.next=pending
    			 * 下次执行线程里的代码时候pending=null了,再下次执行同步代码块就线程阻塞了
    			 * 
    			 * 如果pending属性为空,释放锁的对象监视器,阻塞当前线程
    			 * */ 
    			Reference r;
    			synchronized (lock) {
    				if (pending != null) {
    					r = pending;
    					Reference rn = r.next;
    					pending = (rn == r) ? null : rn;
    					r.next = r;
    				} else {
    					try {
    						lock.wait();
    					} catch (InterruptedException x) { }
    					continue;
    				}
    			}
    			// Fast path for cleaners 
    			if (r instanceof Cleaner) {
    				((Cleaner)r).clean();
    				continue;
    			}
    			/*
    			 *  ReferenceQueue.NULL的实现: 
    			 *  private static class Null extends ReferenceQueue {
    			 * boolean enqueue(Reference r) {
    			 * return false;
  			}
    	}
    			 * 如果Q不为空,把引用放入Queue
    			 * 在刚创建一个引用,第二个参数没放Queue时候,为空。
    			 * */
    			ReferenceQueue q = r.queue;
    			if (q != ReferenceQueue.NULL) q.enqueue(r);
    		}
    	}
    }
    	static {
    		//取得当前线程组
    		ThreadGroup tg = Thread.currentThread().getThreadGroup();
    		//取得最上层的System线程组
    		for (ThreadGroup tgn = tg;
    		tgn != null;
    		tg = tgn, tgn = tg.getParent());
    		//创建线程对象
    		Thread handler = new ReferenceHandler(tg, "Reference Handler");
    		/* If there were a special system-only priority greater than
    		 * MAX_PRIORITY, it would be used here
    		 */
    		handler.setPriority(Thread.MAX_PRIORITY);//设置最高优先级
    		handler.setDaemon(true);//标记守护线程或用户线程
    		handler.start();//守护线程启动
    	}

    	/* -- Referent accessor and setters -- */
    	/**
    	 * Returns this reference object's referent.  If this reference object has
    	 * been cleared, either by the program or by the garbage collector, then
    	 * this method returns <code>null</code>.
    	 *
    	 * @return  The object to which this reference refers, or
    	 *   <code>null</code> if this reference object has been cleared
    	 * 获得引用对象
    	 */
    	public T get() {
    		return this.referent;
    	}
    	/**
	     * Clears this reference object.  Invoking this method will not cause this
	     * object to be enqueued.
	     *
	     * <p> This method is invoked only by Java code; when the garbage collector
	     * clears references it does so directly, without invoking this method.
	     * 清除引用对象
	     */
    	public void clear() {
    		this.referent = null;
    	}

    	/* -- Queue operations -- */
    	/**
    	 * Tells whether or not this reference object has been enqueued, either by
    	 * the program or by the garbage collector.  If this reference object was
    	 * not registered with a queue when it was created, then this method will
    	 * always return <code>false</code>.
	     *
	     * @return  <code>true</code> if and only if this reference object has
	     *   been enqueued
	     */
    	public boolean isEnqueued() {
    		/* In terms of the internal states, this predicate actually tests
    		whether the instance is either Pending or Enqueued 
    		是否处于等待状态
    		判断条件:队列不为空,
         	它的next属性不为空。
         	刚初始化一个Reference时候,next属性肯定是空的,因此肯定不处于等待状态
    		 */
    		synchronized (this) {
    			return (this.queue != ReferenceQueue.NULL) && (this.next != null);
    			}
    		}
    	/**
    	 * Adds this reference object to the queue with which it is registered,
    	 * if any.
    	 *
    	 * <p> This method is invoked only by Java code; when the garbage collector
    	 * enqueues references it does so directly, without invoking this method.
    	 *
    	 * @return  <code>true</code> if this reference object was successfully
    	 *   enqueued; <code>false</code> if it was already enqueued or if
     	*   it was not registered with a queue when it was created
     	* 加入等待队列
     		*/
    	public boolean enqueue() {
    		return this.queue.enqueue(this);
    	}

    	/* -- Constructors -- */
    	Reference(T referent) {
    		this(referent, null);
    	}
    	Reference(T referent, ReferenceQueue<? super T> queue) {
    		this.referent = referent;
    		this.queue = (queue == null) ? ReferenceQueue.NULL : queue;
    	}
	}

 

分享到:
评论
1 楼 leaow567 2012-02-15  
你把这个类里面的信息翻译了一遍,真是感谢,不过,看完之后还是不知道这个类是干嘛用的

相关推荐

    深入探讨 java.lang.ref 包1

    Java.lang.ref 包是 Java 类库中的一个重要组成部分,它包含了与垃圾回收机制密切相关的引用类。这些引用类的设计允许开发者在特定条件下控制对象的生命周期,尤其是在处理缓存、大型数据结构或者避免内存泄漏时显得...

    JavaSE-6.0-英文手册(2008/11/30_FullUpdate)

    java.lang.ref java.lang.reflect java.math java.net java.nio java.nio.channels java.nio.channels.spi java.nio.charset java.nio.charset.spi java.rmi java.rmi.activation java.rmi.dgc java.rmi...

    javajdk源码学习-javaSource:JDK源码学习

    此外,了解`java.lang.ref.Reference`和垃圾收集机制(如可达性分析)也是关键,它们展示了Java内存管理的工作原理。 2. **虚拟机(JVM)内部**:`java.lang.Thread`和`java.lang.Runtime`提供了对JVM内部操作的...

    SCMAD学习笔记

    10. **异常处理**:VM应能处理`NoClassDefFoundError`异常,支持`Java.lang.ref.Reference`(包括`WeakReference`),不包含守护线程,允许调用本地库函数(如`exec()`),提供动态堆栈跟踪,增强`StringBuffer`方法...

    深入探讨 java-lang-ref 包.docx

    总结,java.lang.ref 包中的四种引用类型——StrongReference、SoftReference、WeakReference 和 PhantomReference,提供了不同的内存管理策略,适应不同的场景需求。强引用是最常用的引用类型,但在处理大对象或...

    java 帮助文档 chm格式 java中文帮助文档chm格式.docx

    ##### 3.18 `java.lang.ref` - **用途**:提供引用对象类,支持与垃圾回收器的交互。 - **关键类**:`Reference`, `SoftReference`, `WeakReference` ##### 3.19 `java.lang.reflect` - **用途**:提供用于获取类和...

    java引用的demo

    弱引用通过`java.lang.ref.WeakReference`类表示,即使有弱引用指向对象,只要没有其他强引用,JVM在任何时候都可以回收该对象。这种引用类型在需要临时存储对象,但又不希望影响其生命周期的情况下非常有用。 最后...

    强应用软引用弱引用与垃圾收集的关系1

    - 幻象引用,也称为虚引用,是最弱的引用类型,通过`java.lang.ref.PhantomReference`实现。它不直接指向对象,甚至无法通过幻象引用访问对象。幻象引用的主要用途是在对象被finalize之后,但还没有被垃圾收集之前...

    Java中的软引用弱引用和虚引用.docx

    import java.lang.ref.SoftReference; public class SoftReferenceDemo { public static void main(String[] args) { String str = new String("abc"); SoftReference&lt;String&gt; softReference = new ...

    详解JAVA 虚引用

    在JAVA中,虚引用的使用可以通过PhantomReference类来实现,PhantomReference类继承自java.lang.ref.Reference类,它提供了get()和enqueue()两个方法,get()方法用于获取虚引用的对象实例,enqueue()方法用于将虚...

    api的文档

    **java.lang.ref** - **用途**: 引用对象类。 - **关键类**: - `Reference`: 引用类的基类。 - `SoftReference`: 软引用类。 - `WeakReference`: 弱引用类。 #### 20. **java.lang.reflect** - **用途**: ...

    java7帮助文档

    see Appendix D: Disabling Cryptographic Algorithms in Java PKI Programmer's Guide and Disabled Cryptographic Algorithms in Java Secure Socket Extension (JSSE) Reference Guide. Various enhancements ...

    强,软,弱,虚1

    Java通过`java.lang.ref.SoftReference`类来实现软引用。相比于强引用,软引用提供了一种在内存不足时释放资源的机制,以防止系统崩溃。因此,软引用常用于缓存策略,允许在内存允许的情况下保持缓存,而在内存紧张...

    Java进阶知识点汇总.pdf

    ##### java.lang.ref包下的类 - **Reference**:基类,所有引用类型都继承自该类。 - **PhantomReference**(虚引用):仅用于跟踪垃圾回收过程,不决定对象的生命期。 - **SoftReference**(软引用):当系统即将...

    浅谈Java中的四种引用方式的区别

    Java使用`java.lang.ref.SoftReference`类来表示软引用。 - 当内存空间不足时,JVM会回收软引用关联的对象,以防止`OutOfMemoryError`的发生。软引用常用于实现缓存,如网页缓存和图片缓存,以便在内存紧张时自动...

    理解Java中的弱引用(Weak Reference)

     Java中的弱引用具体指的是java.lang.ref.WeakReference类,我们首先来看一下官方文档对它做的说明:  弱引用对象的存在不会阻止它所指向的对象变被垃圾回收器回收。弱引用常见的用途是实现规范映射...

    Java内存管理之软引用

    软引用(Soft Reference)的主要特点是具有较强的引用功能。只有当内存不够的时候才回收这类内存,因此在内存... import java.lang.ref.SoftReference; public class softReference {  /**  * @param args  *

    Java引用类型具体解释

    JVM 垃圾收集对不同...这种交互方式是使用JDK1.2 引入的 java.lang.ref包。  强引用(strong reference)  在一般的 Java 程序中,见到多的是强引用(strong reference)。如 Date date = newDate(),date 是

    十分钟理解Java中的弱引用编程开发技术共3页.pdf.z

    在Java中,`java.lang.ref`包提供了三种不同类型的引用:强引用(Strong Reference)、软引用(Soft Reference)和弱引用(Weak Reference)。我们主要关注弱引用,它通过`WeakReference`类来实现。弱引用对象在创建...

    referenceInJava:Java强引用,软引用,弱引用以及虚引用测试项目

    软引用通过`java.lang.ref.SoftReference`类实现。 - 示例代码:`SoftReference&lt;String&gt; softRef = new SoftReference(new String("Soft Reference"));` 3. **弱引用(Weak Reference)**: - 弱引用比软引用更弱...

Global site tag (gtag.js) - Google Analytics