Java ConcurentMap keySet () question when a map is modified and iterate over a key set

Fast background I have a parallel map that I used to cache some values ​​that change frequently (it’s worth caching them all the same when testing). I want to regularly export items from my cache, checking the expiration time. I use the keySet () method to get a link to all my keys, then check the values ​​and, if expired, I delete them. In other threads, the cache is requested and updated (deleted) constantly.

From the javadocs for keySet (), he mentions if the map has changed when I repeat over a set of keys. Set the results to undefined. Obviously, I would like to have some way to handle this so that the results are valid. Would it be enough to pass Set to a HashSet and then iterate over this set, since I understand that this set will not be supported by the card, is this a waste of memory? Any ideas appreciated.

Unfortunately, my evictor is not the only way to remove elements from a parallel map, so I correctly say that I need to copy keySet to another set before iterating through it.

Thanks in advance

EDIT: Turns out I read javadocs for the Map keySet () method instead of ConcurrentMap keySet (). Thank you, my bad :)

Returns the given representation of the keys contained in this map. The set is reinforced by the map, so changes to the cards are reflected in the set, and vice versa. If the map is changed while the iteration over the set is in progress (except for the iterator to independently delete the operation), the results of the iteration are undefined. The set supports the delete element, which removes the corresponding display from the display, through Iterator.remove, Set.remove, removeAll retainAll, and clear operations. It does not support add or addAll.

+6
java concurrency map
source share
2 answers

Do you use Java Concurrent HashMap ? From the keySet () documentation, it seems to behave as you see fit.

Returns the specified type of keys contained on this map. The set is supported by the map, therefore changes in the map are reflected in the set and vice versa. The set supports element removal, which removes the matching from this map using the Iterator.remove, Set.remove, removeAll, keepAll, and clear operations. It does not support add or addAll operations. The presented iterator is a “weakly matched” iterator that will never throw a ConcurrentModificationException and guarantees the intersection of elements as they existed during the construction of the iterator, and can (but not guaranteed) reflect any changes after construction.

i.e. you can remove the material and you should be fine.

If you are not using this implementation, which ones are you using (and why? Not funny, but it would be interesting to know why you made this choice)

+6
source share

If the LRU cache is good enough for you, check out LinkedHashMap - this makes the implementation of the LRU cache trivial, and then you can make the results thread safe by wrapping it with a call to Collections.synchronizedMap .

0
source share

All Articles