???????? 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 m = new WeakHashMap();
   
    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 e;
        while ( (e = (Entry) queue.poll()) != null) {
            int hash = e.hash;
            Entry prev = getChain(hash);
            Entry cur = prev;
            while (cur != null) {
                Entry next = cur.next;
                if (cur == e) {
                    if (prev == e)
                        setChain(hash?? next);
                    else
                        prev.next = next;
                    break;
                }
                prev = cur;
                cur = next;
            }
        }
    }


??????????

?????????ú?????????????й???????????????ó???????????????????????????????????????????????????“??????????”??????????£???????????????????й?????????????????????????ú??????????????????????????