WeakHashMap中关于queue的疑惑

是的,“实际上queue中存放的是key所对应的Entry”。因为Reference是异步被enqueue到ReferenceQueue的,它是这么实现的,你可以在java.lang.ref.Reference中找到下面的代码,JVM在加载了这个类并完成初始化之后,就创建了一个ReferenceHandler高优先级的线程,用于将待处理的引用加入到队列之中:
/* High-priority thread to enqueue pending References */ private static class ReferenceHandler extends Thread { private static void ensureClassInitialized(Class\u0026lt;?\u0026gt; clazz) { try { Class.forName(clazz.getName(), true, clazz.getClassLoader()); } catch (ClassNotFoundException e) { throw (Error) new NoClassDefFoundError(e.getMessage()).initCause(e); } } static { // pre-load and initialize InterruptedException and Cleaner classes // so that we don\u0026#39;t get into trouble later in the run loop if there\u0026#39;s // memory shortage while loading/initializing them lazily. ensureClassInitialized(InterruptedException.class); ensureClassInitialized(Cleaner.class); } ReferenceHandler(ThreadGroup g, String name) { super(g, name); } public void run() { while (true) { tryHandlePending(true); } } } /** * Try handle pending {@link Reference} if there is one.\u0026lt;p\u0026gt; * Return {@code true} as a hint that there might be another * {@link Reference} pending or {@code false} when there are no more pending * {@link Reference}s at the moment and the program can do some other * useful work instead of looping. * * @param waitForNotify if {@code true} and there was no pending * {@link Reference}, wait until notified from VM * or interrupted; if {@code false}, return immediately * when there is no pending {@link Reference}. * @return {@code true} if there was a {@link Reference} pending and it * was processed, or we waited for notification and either got it * or thread was interrupted before being notified; * {@code false} otherwise. */ static boolean tryHandlePending(boolean waitForNotify) { Reference\u0026lt;Object\u0026gt; r; Cleaner c; try { synchronized (lock) { if (pending != null) { r = pending; // \u0026#39;instanceof\u0026#39; might throw OutOfMemoryError sometimes // so do this before un-linking \u0026#39;r\u0026#39; from the \u0026#39;pending\u0026#39; chain... c = r instanceof Cleaner ? (Cleaner) r : null; // unlink \u0026#39;r\u0026#39; from \u0026#39;pending\u0026#39; chain pending = r.discovered; r.discovered = null; } else { // The waiting on the lock may cause an OutOfMemoryError // because it may try to allocate exception objects. if (waitForNotify) { lock.wait(); } // retry if waited return waitForNotify; } } } catch (OutOfMemoryError x) { // Give other threads CPU time so they hopefully drop some live references // and GC reclaims some space. // Also prevent CPU intensive spinning in case \u0026#39;r instanceof Cleaner\u0026#39; above // persistently throws OOME for some time... Thread.yield(); // retry return true; } catch (InterruptedException x) { // retry return true; } // Fast path for cleaners if (c != null) { c.clean(); return true; } ReferenceQueue\u0026lt;? super Object\u0026gt; q = r.queue; if (q != ReferenceQueue.NULL) q.enqueue(r); return true; } static { ThreadGroup tg = Thread.currentThread().getThreadGroup(); for (ThreadGroup tgn = tg; tgn != null; tg = tgn, tgn = tg.getParent()); Thread handler = new ReferenceHandler(tg, "Reference Handler"); /* If there were a special system-only priority greater than * MAX_PRIORITY, it would be used here */ handler.setPriority(Thread.MAX_PRIORITY); handler.setDaemon(true); handler.start(); // provide access in SharedSecrets SharedSecrets.setJavaLangRefAccess(new JavaLangRefAccess() { @Override public boolean tryHandlePendingReference() { return tryHandlePending(false); } }); }


推荐阅读