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); } }); }
推荐阅读
- 过节■江苏省委省政府办公厅下发关于做好2021年元旦春节期间有关工作的通知
- |徐州市出台《关于优化创新创业生态系统 提升区域科技创新活力的实施意见》及实施细则
- 雨下|全球关于禁售燃油车只是理论上可行吗
- 关于用phpfsocket 写Post, 模拟http 报文怎样写入要传输的处理数据
- 智叔|很多家长还在整箱买:谈谈关于牛奶的17个真相警惕这些列入黑名单的“假牛奶”
- 关于微信小程序的思考:运营者该何去何从
- 关于人工智能虚拟人的一些问题
- 知乎上关于人生经验的介绍是否可能对青少年造成潜在危害
- 写下我关于做数据分析专员的困惑和各位的建议是
- 非物质文化遗产|市北文体中心一场关于“非遗”的盛会…
