WeakedHashMap 的例子

WeakedHashMap會在key對象已經沒有引用時,若是調用System.gc()會嘗試remove這個key
import java.util.*;

public class WeakedHashMap {
    public static void main(String args[]){
    	Map<String, String> map = new WeakHashMap<String, String>();
    	//Map<String, String> map = new HashMap<String, String>();  
    	String alibaba = new String("TEST");
        map.put(alibaba, "TEST");   
        while (map.containsKey("TEST")) {   
            try {   
                Thread.sleep(500);   
                alibaba = null;
             } catch (InterruptedException ignored) {   
             }   
             System.out.println("Checking for empty");   
             System.gc();   
        }  
    }
}
相關文章
相關標籤/搜索