The below code is throwing concurrent Modification exception , the line where the exception is pointing is the first line of for loop
private synchronized void updateAllCacheValues() {
for (Map.Entry<Configurations, SalesConfiguration> entry : ConfigurationCache.entrySet()) {
Configurations conf = entry.getKey();
SalesConfiguration saleConfiguration = ConfigurationCache.get(conf);
Map<String, String> newMap = generateKeyValueMapFromConfigurations(conf);
lastLoadTimestamp = new Date();
saleConfiguration.setMap(newMap, lastLoadTimestamp);
}
logger.debug("Successfully updated all cached configurations., cache size " + ConfigurationCache.size() + "LAST_LOAD_TIME" + lastLoadTimestamp);
}
Below is the exception trace
java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1429)
at java.util.HashMap$EntryIterator.next(HashMap.java:1463)
at java.util.HashMap$EntryIterator.next(HashMap.java:1461)
at com.learning.java.daily.updateAllCacheValues(ConfigurationLoader.java:237)
at com.learning.java.daily.updateAllCacheValues.impl.ConfigurationLoader.loadConfigurations(ConfigurationLoader.java:156)
I am not able to guess what could have caused this exception , because I wrote a sample test where I am modifying the attributes of the value of Map but concurrent modification exception did not appear.
To answer the question in your title: No, modifying value objects in your HashMap
while iterating the entry set cannot alone produce a ConcurrentModificationException
. There must be something else going on.
Could either the call to generateKeyValueMapFromConfigurations()
or to saleConfiguration.setMap()
modify the map? Might it be that your ConfigurationCache
could be modified by some other, concurrent thread?? I know I am just guessing, it’s the best we can do with the information at hand.