How to find out the value associated with a remote entry in the WeakHashMap file

I have something like this:

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

MyObj1 can be removed when the GC enters the game and there is no strong link to it.

I would like to pass checkThis(MyObj2) specific map value object associated with the remote key (maybe check ReferenceQueue ?)

I cannot figure out how to do this in code.

+4
source share
4 answers

From the comment to another answer:

I want that after the end of the session (and there is no strong link to this session in my application context) The list is repeated and canceled. called on all other futures that are awaiting execution. This avoids scheduled tasks that will work even if the user session has expired.

This sounds like a job to the HttpSessionBindingListener

+1
source

The real problem is what you want to do is that WeakHashMap relies on a garbage collector to release unaccepted items.

Since the GC will do its job whenever it wants, without any respect for what you are doing, it will be difficult to track changes in the hashmap. Actually what you say

"MyObj1 button is deleted if there is no strong link to it"

not accurate. MyObj1 can be released by GC if it does not have strong links, but if there is no need to release it, it simply will not be released.

0
source

From the WeakHashMap preamble (italics added):

Implementation of a map based on a hash table with weak keys. An entry in WeakHashMap will be automatically deleted if its key is no longer used in the usual way. More precisely, the presence of a mapping for a given key will not prevent the key from being discarded by the garbage collector, i.e. it will be completed, finalized, and then regenerated. When a key has been discarded, its record is effectively deleted from the map, therefore this class behaves somewhat differently than other Map implementations.

However, you never told The WeakHashMap when it "loses" the record :-) If you only care about keys for a specific instance on time, you can use a map scan (also applicable only for some n). Otherwise, go to the question about ReferenceQueue.

This link may be useful: Java Reference Objects

0
source

Link Queues

As soon as the WeakReference function starts returning null, the object it pointed to becomes garbage, and the WeakReference object is practically useless. This usually means that some kind of cleaning is required; For example, WeakHashMap must delete such non-existent entries in order to avoid holding onto the ever-increasing number of dead WeakReferences.

The ReferenceQueue class makes tracking dead links easier. If you pass the ReferenceQueue to a weak link builder, the link object will automatically be inserted into the link queue when the object it points to becomes garbage. You can then process the ReferenceQueue at some regular interval and do any cleanup for dead links.

See this page for usage guide.

Could you please indicate why you are using this? Very few valid uses.
i.e. cache is invalid (or at least not good)

Edit:

This code is equivalent to using weakHashMap, but you need to explicitly do this to adjust the queue with the map.

 HashMap aHashMap = new HashMap(); ReferenceQueue Queue = new ReferenceQueue(); MyWeakReference RefKey = new MyWeakReference(key, Queue); aHashMap.put(RefKey, value); 
0
source

Source: https://habr.com/ru/post/1314223/


All Articles