强引用、软引用、弱引用、虚引用的概念、区别、应用

2017-04-16  本文已影响0人  漠简尘

一、四大引用级别的概念

二、四大引用级别之间的区别

强引用和软引用

软引用和弱引用

弱引用和虚引用

package com;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
public class Main {
    static Weak weak;
    static class Weak {
        @Override
        protected void finalize() throws Throwable {
            super.finalize();
            System.out.println("finalize() is called");
            weak = this;//复活对象
            System.out.println("after finalize ,object is alive again");
        }
    }

    public static void main(String[] args) throws InterruptedException {
        weak = new Weak();
        ReferenceQueue queue = new ReferenceQueue<>();
        WeakReference<Weak> weakReference = new WeakReference<Weak>(weak, queue);
//        PhantomReference<Weak> weakReference = new PhantomReference<>(weak,queue);
        if (weakReference.get() == null) {
            System.out.println("before gc : reference is not available");
        } else {
            System.out.println("before gc : reference is available");
        }
        weak = null;
        System.gc();//执行GC线程
        Thread.sleep(3000);
        if (weakReference.get() == null) {
            System.out.println("after gc : reference is not available");
        } else {
            System.out.println("after gc : reference is available");
        }
        if (queue.poll() == null) {
            System.out.println("after gc : reference is not in queue");
        } else {
            System.out.println("after gc : reference is in queue");
        }
        weak=null;
        System.gc();//再次执行GC
        Thread.sleep(3000);
        if (queue.poll() == null) {
            System.out.println("gc agaain : reference is not in queue");
        } else {
            System.out.println("gc agaain : reference is in queue");
        }
  }    
}

三、四大引用的应用

软引用

package com;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.util.HashMap;
public class Main {
}
class People{
    String id;
    String name;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
class PeopleCache {
    private static PeopleCache singlonCache;
    private HashMap<String, PeoReference> refChache;//用来存储缓存的数据
    private ReferenceQueue queue;
    //自定义引用类,增加属性_key,方便在缓存数据中查找
    static class PeoReference extends SoftReference {
        private String _key;
        public PeoReference(People referent, ReferenceQueue q) {
            super(referent, q);
            _key = referent.getId();
        }
    }
    private PeopleCache(){
        this.queue=new ReferenceQueue();
        this.refChache=new HashMap<>();
    }
    public static PeopleCache getInstance(){
        if(singlonCache==null){
            singlonCache=new PeopleCache();
        }
        return singlonCache;
    }
    public void cachePeople(People people){
        cleanCache();//清除已经标记为垃圾的引用
        PeoReference reference = new PeoReference(people, queue);
        refChache.put(people.getId(), reference);//将对象的软引用保存到缓存中
    }
    public void cleanCache(){
        PeoReference reference = null;
        while ((reference = (PeoReference)queue.poll())!=null){
            refChache.remove(reference._key);
        }
    }
    public People getCachedPeople(String key){
        People people = null;
        if (refChache.containsKey(key)){
            people= (People) refChache.get(key).get();
            System.out.println("get object from cache");
        }else{
            people = new People();
            System.out.println("get object from database or web server");
        }
        return people;
    }
}

弱引用

虚引用

上一篇 下一篇

猜你喜欢

热点阅读