简体   繁体   English

HashMap ConcurrentModificationException同时初始化另一个HashMap

[英]HashMap ConcurrentModificationException while initializing with another HashMap

In a multi-threaded code, I am seeing ConcurrentModificationException on line 3 在多线程代码中,我在第3行看到ConcurrentModificationException

line 1:   Map<String, String> attributMap = new HashMap<>();
line 2:   if(attributeMap.size() > 0)
line 3:       tagMyEvent(new HashMap<>(attributeMap));

java.util.ConcurrentModificationException
    1   at java.util.HashMap$HashIterator.nextEntry(HashMap.java:851)
    2   at java.util.HashMap$EntryIterator.next(HashMap.java:891)
    3   at java.util.HashMap$EntryIterator.next(HashMap.java:890)
    4   at java.util.HashMap.putAllForCreate(HashMap.java:485)
    5   at java.util.HashMap.<init>(HashMap.java:257)
!   6   at tagMyEvent (test.java:line 3)  

Only one reason for this crash I could guess is: 我能猜出此崩溃的唯一原因是:

  • attributMap is being modified when the new HashMap<>(attributMap) is being created. 创建新的HashMap <>(attributMap)时将修改attributMap。

Will changing of the above code to this, solve the problem: 为此将上面的代码更改,解决问题:

line 1:   Map<String, String> attributMap = new HashMap<>();
line 2:   if(attributeMap.size() > 0)
line 3:       tagMyEvent(new ConcurrentHashMap<>(attributeMap));

OR 要么

line 1:   Map<String, String> attributMap = new ConcurrentHashMap<>();
line 2:   if(attributeMap.size() > 0)
line 3:       tagMyEvent(new ConcurrentHashMap<>(attributeMap));

If not, can someone suggest a solution or throw some light on what exactly is causing this problem. 如果不是,那么有人可以提出解决方案还是对导致此问题的确切原因有所了解。

Thanks in advance. 提前致谢。

If anything, "attributeMap" is the one that needs to be concurrent - because if it's just a regular HashMap, it would still refuse to be iterated upon inside "new ConcurrentHashMap", and we've achieved nothing... 如果有的话,“ attributeMap”是需要并发的-因为如果它只是一个常规的HashMap,它仍然会拒绝在“新的ConcurrentHashMap”内部进行迭代,并且我们什么都没有实现……

That said, I'd also kindly suggest 话虽如此,我也建议

  1. Consider whether ConcurrentHashMap is really good enough for your needs, that depends what other operations you run on it, and what's the level of cooperation between threads, or how immediately they should see each other's changes... 考虑一下ConcurrentHashMap是否真的足够满足您的需求,这取决于您在其上执行了哪些其他操作,线程之间的协作程度如何,或者线程应该如何立即看到彼此的更改...
  2. I assume we only saw a partial code snippet (obviously you're not initializing "attributeMap" locally in a method, because then there would be no concurrency problem). 我假设我们只看到了部分代码片段(显然您没有在方法中本地初始化“ attributeMap”,因为那样就不会有并发问题)。 So consider testing for other problems as well - eg if the complete code does some additional manipulation that was omitted from this post for simplicity. 因此,也考虑测试其他问题-例如,如果完整的代码执行了一些其他操作(为简单起见,本文中省略了这些操作)。 Remember that some manipulations could yield a ConcurrentModificationException even if you only have a single thread... 请记住,即使您只有一个线程,某些操作也会产生ConcurrentModificationException ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM