私はこのようなものを持っています:
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 が登場すると削除される可能性があり、それへの強い参照はありません。
削除されたキーに関連付けられた特定のマップ値オブジェクトに渡したいcheckThis(MyObj2)
(おそらくReferenceQueue
?)
これをコードに入れる方法がわかりません。