java – 如何知道与WeakHashMap中已删除条目关联的值
发布时间:2020-12-15 02:20:54 所属栏目:Java 来源:网络整理
导读:我有这样的事情: private MapMyObj1,MyObj2 map = new WeakHashMapMyObj1,MyObj2(); ... somewhere in the code ... MyObj1 myObj1 = new MyObj1(); map.put(myObj1,new MyObj2(); ... myObj1 = null;... somewhere else in a thread ... (I would like to
我有这样的事情:
private Map<MyObj1,MyObj2> map = new WeakHashMap<MyObj1,MyObj2>(); ... somewhere in the code ... MyObj1 myObj1 = new MyObj1(); map.put(myObj1,new MyObj2(); ... myObj1 = null; ... somewhere else in a thread ... (I would like to pass to a checkThis(MyObj2) method the Value associated with the entry that was removed from the Map) /* something like this maybe */ while (true) { MyObj2 myObj2 = referenceQueue.remove().get(); checkThis(myObj2); } GC进场时可能会删除MyObj1键,并且没有强烈的引用. 我想传递给checkThis(MyObj2)与被删除的键相关联的特定map-value对象(可能检查一个ReferenceQueue?) 我无法弄清楚如何将其放入代码中. 解决方法
从另一个答案的评论:
这听起来像 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |