我有这样的事情:
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);
}
MyObj1
当 GC 发挥作用并且没有强引用它时,key 可能会被删除。
我想传递给与checkThis(MyObj2)
被删除的键关联的特定映射值对象(也许检查一个ReferenceQueue
?)
我无法弄清楚如何将其放入代码中。