Java????????WeakHashMap
???????????? ???????[ 2012/7/30 13:10:29 ] ????????
????WeakHashMap?????????е?expungeStaleEntries()???????????????з????б????á??????????ReferenceQueue???????Ч???????Map???????
private void expungeStaleEntries() {
Entry<K??V> e;
while ( (e = (Entry<K??V>) queue.poll()) != null) {
int h = e.hash;
int i = indexFor(h?? table.length);
Entry<K??V> prev = table[i];
Entry<K??V> p = prev;
while (p != null) {
Entry<K??V> next = p.next;
if (p == e) {
if (prev == e)
table[i] = next;
else
prev.next = next;
e.next = null; // Help GC
e.value = null; // " "
size--;
break;
}
prev = p;
p = next;
}
}
}
????3???????????????
????WeakHashMap??Key?????????Value?????
????WeakHashMap???????????Ч???????????????????expungeStaleEntries()????з???????????????????
????4?????????????
public static void main(String args[]) {
WeakHashMap<String?? String> map = new WeakHashMap<String?? String>();
map.put(new String("1")?? "1");
map.put("2"?? "2");
String s = new String("3");
map.put(s?? "3");
while (map.size() > 0) {
try {
Thread.sleep(500);
} catch (InterruptedException ignored) {
}
System.out.println("Map Size:"+map.size());
System.out.println(map.get("1"));
System.out.println(map.get("2"));
System.out.println(map.get("3"));
System.gc();
}
}
???????н?????
Map Size:3
1
2
3
Map Size:2
null
2
3
Map Size:2
null
2
3
?????????
????????String?????????“2”?????????????е????????б??????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11