Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As bkail said, when GC "removes" an entry from a <code>WeakHashMap</code> it is not going to cause a concurrent modification. In reality the GC collects the underlying object by there is a hard reference to <code>WeakReference</code> object (that holds the real key) itself. Therefore, the real object (the reference object) that is directly referenced by the map is not collected so the map does not change until one of your threads calls a method in this map. At that time the map checks the reference queue from the GC and finds all keys that have been collected and removes them from the map - so the actual changes to the map structure occurs on one of your threads.</p> <p>In thinking of this then there may be one case where you may get a concurrent modification in such a map that you would not get in another kind of map - if you put a key that already exists or call a getter method. But really, in a concurrent application you should be locking around these calls anyway so you would have a really concurrent access bug in your program.</p> <p>That being said in answer to your question, you really <strong>should not use</strong> <code>WeakHashMap</code> for a cache (even if you were talking about caching keys). In a cache you don't want your values 'magically' disappearing when the values are no longer referenced. Typically you want them to disappear when there is a certain maximum number (something like Apache collections <a href="http://commons.apache.org/collections/apidocs/org/apache/commons/collections/LRUMap.html" rel="noreferrer"><code>LRUMap</code></a>) or released on memory demand. </p> <p>For the later, you can use a map with a <code>SoftReference</code> (Apache collections provides a <a href="http://commons.apache.org/collections/apidocs/org/apache/commons/collections/map/ReferenceMap.html" rel="noreferrer"><code>ReferenceMap</code></a> that allows you to specify the kind of reference for either the key or value). A soft reference is specified to only be released based on memory pressure - a weak reference on the other hand has to do more with GC recognizing that there are no hard reference remaining to the object and can release it at any time. Of course, how a soft reference really works is also dependent on the JVM implementation.</p> <p><strong>EDIT</strong>: I reread your question and want to address one other point. Because the actual modifications occur to the <code>WeakHashMap</code> internal structures on your own thread, <em>if you only use this map in a single thread you do not need to synchronize any method calls</em>. This behavior is no different than any other <code>Map</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload