码迷,mamicode.com
首页 > 编程语言 > 详细

thinking in java 之Reference类的使用

时间:2016-05-24 20:59:20      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:

Reference是java中的特殊引用类。描述的是特殊作用(主要是关于垃圾回收对象)的引用。

它有3个子类:

1.SoftReference;

2.WeakReference

3.PhantomReference

先看thinking in java 中的实例

package containers;

import java.lang.ref.PhantomReference;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.LinkedList;

//对引用的 描述与封装,Reference类

class VeryBig2{
    private static final int SIZE= 10000;
    private long[] la= new long[SIZE];
    private String ident;
    public VeryBig2(String id){
        ident= id;
    }
    @Override
    public String toString() {
        
        return ident;
    }
    protected void finalize(){
        System.out.println("Finalizing "+ ident);
    }
}

public class ReferencesTest {
    private static ReferenceQueue<VeryBig2> rq= 
            new ReferenceQueue<VeryBig2>();
    public static void checkQueue(){
        Reference<? extends VeryBig2> inq= rq.poll();
        if (inq != null) {
            System.out.println("In queue: "+ inq.get());
        }
    }
    
    public static void main(String[] args) {
        int size= 10;
        if (args.length> 0) {
            size= new Integer(args[0]);
        }
        LinkedList<SoftReference<VeryBig2>> sa= 
                new LinkedList<SoftReference<VeryBig2>>();
        for (int i = 0; i < size; i++) {
            sa.add(new SoftReference<VeryBig2>(new VeryBig2("Soft "+ i), rq));
            System.out.println("Just created: "+ sa.getLast());
            checkQueue();
        }
        
        LinkedList<WeakReference<VeryBig2>> wa= 
                new LinkedList<WeakReference<VeryBig2>>();
        for (int i = 0; i < size; i++) {
            wa.add(new WeakReference<VeryBig2>(new VeryBig2("Weak "+ i), rq));
            System.out.println("Just created: "+ wa.getLast());
            checkQueue();
        }
        
        SoftReference<VeryBig2> s= 
                new SoftReference<VeryBig2>(new VeryBig2("Soft"));
        WeakReference<VeryBig2> w= 
                new WeakReference<VeryBig2>(new VeryBig2("Weak"));
        System.gc();//对无引用的对象,执行垃圾回收操作。并且,调用回收对象的finalize方法
        
        LinkedList<PhantomReference<VeryBig2>> pa= 
                new LinkedList<PhantomReference<VeryBig2>>();
        for (int i = 0; i < size; i++) {
            pa.add(new PhantomReference<VeryBig2>(new VeryBig2("Phantom "+ i), rq));
            System.out.println("Just created: "+ pa.getLast());
            checkQueue();
        }
    }
}

output:

Just created: java.lang.ref.SoftReference@2a9931f5
Just created: java.lang.ref.SoftReference@2f9ee1ac
Just created: java.lang.ref.SoftReference@67f1fba0
Just created: java.lang.ref.SoftReference@3fbefab0
Just created: java.lang.ref.SoftReference@133c5982
Just created: java.lang.ref.SoftReference@5f186fab
Just created: java.lang.ref.SoftReference@3d4b7453
Just created: java.lang.ref.SoftReference@24c21495
Just created: java.lang.ref.SoftReference@41d5550d
Just created: java.lang.ref.SoftReference@1cc2ea3f
Just created: java.lang.ref.WeakReference@40a0dcd9
Just created: java.lang.ref.WeakReference@1034bb5
Just created: java.lang.ref.WeakReference@7f5f5897
Just created: java.lang.ref.WeakReference@4cb162d5
Just created: java.lang.ref.WeakReference@11cfb549
Just created: java.lang.ref.WeakReference@5b86d4c1
Just created: java.lang.ref.WeakReference@70f9f9d8
Just created: java.lang.ref.WeakReference@2b820dda
Just created: java.lang.ref.WeakReference@675b7986
Just created: java.lang.ref.WeakReference@2687816d
Just created: java.lang.ref.PhantomReference@7b11a3ac
In queue: null
Just created: java.lang.ref.PhantomReference@7ca83b8a
In queue: null
Finalizing Weak
Just created: java.lang.ref.PhantomReference@8dd20f6
In queue: null
Just created: java.lang.ref.PhantomReference@6d9efb05
In queue: null
Just created: java.lang.ref.PhantomReference@60723d7c
In queue: null
Just created: java.lang.ref.PhantomReference@6c22c95b
In queue: null
Just created: java.lang.ref.PhantomReference@5fd1acd3
In queue: null
Just created: java.lang.ref.PhantomReference@3ea981ca
In queue: null
Finalizing Weak 9
Just created: java.lang.ref.PhantomReference@6a8814e9
In queue: null
Finalizing Weak 8
Finalizing Weak 7
Just created: java.lang.ref.PhantomReference@c1503a3
In queue: null
Finalizing Weak 6
Finalizing Weak 5
Finalizing Weak 4
Finalizing Weak 3

从结果中,可以看出:

1.垃圾回收器回收优先级别:SoftReference< WeakReference < PhantomReference。

2.当运行垃圾回收方法System.gc时,会先调用目标对象的finalize方法。

3.

thinking in java 之Reference类的使用

标签:

原文地址:http://www.cnblogs.com/westward/p/5524507.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!