首页   快速返回

软引用弱引用及引用队列实战例子     所属分类 java
ConcurrentHashMap value 为 软引用 或 弱引用 实现 MyRef接口
MyRef接口 用于 获取关联的 key 

软/弱引用关联引用队列

引用回收时计入引用队列

后台线程循环获取 引用队列里的 引用对象,通过getKey 获取对应的 key 
remove ConcurrentHashMap 里的 key

jvm参数
 -Xms1024m -Xmx1024m -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -Xloggc:/Users/dugang/gclogs/reftestgc.log	
 
 查看 gc 回收日志
 

完整代码



import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

public class RefqTest{

	
    private final static ReferenceQueue<Object> refq = new ReferenceQueue<>();

    private final static AtomicLong seq = new AtomicLong(0);
    private final static AtomicLong removeCount = new AtomicLong(0);

    
    private final static Map<String,MyRef> map = new ConcurrentHashMap<String,MyRef>();
    
    private static final String VALUE_POSTFIX = getValuePostfix();
    
    
	public static void main(String[] args) throws Exception {

		String key = null;
		String value = null;
		
		RefqThread t = new RefqThread();
		t.start();
		
	    //  -Xms1024m -Xmx1024m -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -Xloggc:/Users/dugang/gclogs/reftestgc.log	
		MyRef ref = null;
		
		while(true){
			key = "key-"+seq.getAndIncrement();
			value = key+"-"+VALUE_POSTFIX;
			ref = new MyWeakRef(key,value);
			// ref = new MySoftRef(key,value);
			map.put(key, ref);
		}
			
	}
	
	
	
	private static final String getValuePostfix(){
		String str = "";
		for(int i=0;i<100000;i++){
			str = str + "X";
		}
		return str;
	}
	
	private static interface MyRef{
		public String getKey();
	}
	
	private static class MyWeakRef extends WeakReference<String> implements MyRef{
		public String key;
		
		public MyWeakRef(String key,String value){
			// 注意关联引用队列 !!!
			super(value,refq);
			// super(value);
			this.key = key;
		}
		
		@Override
		public String getKey(){
			return key;
		}
		
		@Override
		public String toString(){
			return "key="+key;
		}
			
	}
	
	private static class MySoftRef extends SoftReference<String> implements MyRef{
		public String key;
		
		public MySoftRef(String key,String value){
			// 注意关联引用队列 !!!
			super(value,refq);
			// super(value);
			this.key = key;
		}
		
		@Override
		public String getKey(){
			return key;
		}
		
		@Override
		public String toString(){
			return "key="+key;
		}
			
	}
	
	
	private static class RefqThread extends Thread{
		public void run(){
			MyRef ref = null;
			
			System.out.println(this+",start");
			long count = 0;
			while(true){
				ref = (MyRef)refq.poll();
				if(ref==null){
					Thread.yield();			
					continue;
				}
				map.remove(ref.getKey());
				count = removeCount.incrementAndGet();
				
				if(count % 10000 == 0){
					System.out.println(ref+",removeCount="+count);     
				}			
			}
		}
	}

}



上一篇     下一篇
prometheus集成grafana实现可视化

g1 GC 要点

四种引用及WeakHashMap介绍

grafana基本概念

跨域资源共享CORS介绍

Elastic Stack 7.0.0 特性介绍