Java ????????????????????????й?
???????????? ???????[ 2012/7/26 11:03:26 ] ????????
???????? WeakReference.get() ?????????????? referent ????????????????????????????????????????? while ????????????????????????????????????WeakHashMap ???????????????????????÷? —— ?Щ?????????? WeakReference?????????????????????????????????????
???????? WeakHashMap ???????????????????????????“????”?????????????????????????????£?get() ???? null??????ò??? get() ?????????? null ??????????????
?????? WeakHashMap ???й?
?????? SocketManager ?з??й???????????? WeakHashMap ???? HashMap ????????嵥 6 ?????????? SocketManager ??????????????????? Collections.synchronizedMap() ??? WeakHashMap??????????????????????????????????????????????????????????????????????С????????????????????????????????????? HashMap ??? Map ??????
?????嵥 6. ?? WeakHashMap ??? SocketManager
public class SocketManager {
private Map
public void setUser(Socket s?? User u) {
m.put(s?? u);
}
public User getUser(Socket s) {
return m.get(s);
}
}
???????????
????WeakHashMap ???????ó????????????????ó???????ü???????????????????????get() ????????? WeakReference.get() ????? null ?????????????????????????????? Map ?????????????ó?????????????в????????????????????????????????Щ???????????????????? Map ????????????Map ???????????????????????????ó????????????????????????????ó???????棬????????????????Map.Entry ?????????????????
????????????????????? Map????????????????? get()?????? get() ???? null ??????????????????????????? Map ?????????????????????Ч????????????????????????????? referent ???????????????????????????????? ?????á?
??????????????????????????ó??????????????????????????????????????????????????????????? referent ?????????????????????????????????????ù???????????д???????????? referent ??? GC ???????????????????????? referent???????????????? ??????????С??????ó?????????????????ò???????? referent ??????????????????????????????????????????????е????????????????????? BlockingQueue ?????????? —— polled??timed blocking ?? untimed blocking????
????WeakHashMap ???????? expungeStaleEntries() ????з?????????? Map ?????л???????????????????????????Ч??????????????????????嵥 7 ???? expungeStaleEntries() ????????????????洢??-????? Entry ????????? WeakReference?????? expungeStaleEntries() ?????????Ч??????????????????? Entry??????????д??漲??????????????????? Map ????Ч?????????????????????????????????????е???????????????
?????嵥 7. WeakHashMap.expungeStaleEntries() ????????
private void expungeStaleEntries() {
Entry
while ( (e = (Entry
int hash = e.hash;
Entry
Entry
while (cur != null) {
Entry
if (cur == e) {
if (prev == e)
setChain(hash?? next);
else
prev.next = next;
break;
}
prev = cur;
cur = next;
}
}
}
??????????
?????????ú?????????????й???????????????ó???????????????????????????????????????????????????“??????????”??????????£???????????????????й?????????????????????????ú??????????????????????????
??????
???·???
??????????????????
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