Java編程WeakHashMap實例解析
簡述:
《Thinking in Java》第4版 P519 頁 WeakHashMap一章讀書筆記
WeakHashMap 用來保存WeakReference,這一結(jié)構(gòu)云遜垃圾回收器自動清理鍵和值
在添加鍵和值的操作時,映射會自動使用WeakReference包裝它們,
見jdk源代碼,
public V put(K key, V value) {
Object k = maskNull(key);
int h = hash(k);
Entry<K,V>[] tab = getTable();
int i = indexFor(h, tab.length);
for (Entry<K,V> e = tab[i]; e != null; e = e.next) {
if (h == e.hash && eq(k, e.get())) {
V oldValue = e.value;
if (value != oldValue)
e.value = value;
return oldValue;
}
}
modCount++;
Entry<K,V> e = tab[i];
tab[i] = new Entry<>(k, value, queue, h, e);
if (++size >= threshold)
resize(tab.length * 2);
return null;
}
其中new Entry<>(k, value, queue, h, e)一行使用了ReferenceQueue
/** * Reference queue for cleared WeakEntries */ private final ReferenceQueue<Object> queue = new ReferenceQueue<>();
點入new Entry的構(gòu)造函數(shù),進入super頂層可以看到,
/**
* Creates a new weak reference that refers to the given object and is
* registered with the given queue.
*
* @param referent object the new weak reference will refer to
* @param q the queue with which the reference is to be registered,
* or <tt>null</tt> if registration is not required
*/
public WeakReference(T referent, ReferenceQueue<? super T> q) {
super(referent, q);
}
這里new Entry同時也構(gòu)造出來了一個WeakRefence對象
測試:
package com.anialy.test.data_structure.map;
import java.util.Iterator;
import java.util.WeakHashMap;
public class WeakHashMapTest {
public static void main(String[] args) {
WeakHashMap wmap = new WeakHashMap<String, Object>();
final int SIZE = 10;
String[] str = new String[SIZE];
for (int i=0; i<SIZE; i++){
String key = Integer.toString(i);
String value = Integer.toString(i);
// 每隔3個保留一個引用
if(i % 3 == 0)
str[i] = key;
wmap.put(key, value);
}
System.gc();
Iterator iter = wmap.keySet().iterator();
while(iter.hasNext()){
System.out.println(wmap.get(iter.next()));
}
}
}
可以預(yù)料到,部分由于String[] 保留了弱引用,所以輸出都是間隔3的
總結(jié)
以上就是本文關(guān)于Java編程WeakHashMap實例解析的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
欄 目:Java編程
下一篇:Java編程實現(xiàn)beta分布的采樣或抽樣實例代碼
本文地址:http://www.jygsgssxh.com/a1/Javabiancheng/8352.html
您可能感興趣的文章
- 01-10Java咖啡館(1)——嘆咖啡
- 01-10Java Socket編程(三) 服務(wù)器Sockets
- 01-10Java進階:Struts多模塊的技巧
- 01-10Java Socket編程(一) Socket傳輸模式
- 01-10Java Socket編程(二) Java面向連接的類
- 01-10Java運行時多態(tài)性的實現(xiàn)
- 01-10Java經(jīng)驗點滴:處理沒有被捕獲的異常
- 01-10Java Socket編程(四) 重復(fù)和并發(fā)服務(wù)器
- 01-10Java中的浮點數(shù)分析
- 01-10面向?qū)ο缶幊?Java中的抽象數(shù)據(jù)類型


閱讀排行
本欄相關(guān)
- 01-10Java咖啡館(1)——嘆咖啡
- 01-10JVM的垃圾回收機制詳解和調(diào)優(yōu)
- 01-10Java Socket編程(三) 服務(wù)器Sockets
- 01-10Java進階:Struts多模塊的技巧
- 01-10J2SE 1.5版本的新特性一覽
- 01-10Java Socket編程(一) Socket傳輸模式
- 01-10Java運行時多態(tài)性的實現(xiàn)
- 01-10Java Socket編程(二) Java面向連接的類
- 01-10Java Socket編程(四) 重復(fù)和并發(fā)服務(wù)
- 01-10Java經(jīng)驗點滴:處理沒有被捕獲的異常
隨機閱讀
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實例總結(jié)
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11ajax實現(xiàn)頁面的局部加載


