public static void main(String[] args){
Map<String, String> syncMap = Collections.synchronizedMap(new HashMap<String, String>());
syncMap.put("1","1");
syncMap.put("2","2");
syncMap.put("3","3");
for (String key : syncMap.keySet()){
addValue(syncMap);
}
}
private static void addValue(Map<String, String> map){
map.put("4", "4");
}
Throws ConcurrentModificationException
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1429)
at java.util.HashMap$KeyIterator.next(HashMap.java:1453)
ConcurrentHashMap never throws ConcurrentModificationException
Map<String, String> syncMap = new ConcurrentHashMap<>();
No comments:
Post a Comment