Java – how do I know the value associated with a deleted entry in the weakhashmap

I have such things:

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);
}

The myobj1 key may be deleted when GC comes into play, and there is no strong reference

I want to pass checkthis (myobj2) to the specific map value object associated with the deleted key (may I check a ReferenceQueue?)

I can't figure out how to put it in the code

Solution

Comments from another answer:

This sounds like the work of HttpSessionBindingListener

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>