java HashMap中的ConcurrentModificationException程序
代码:
Map<Integer,DealCountUpdater> dealCountMap=new HashMap<Integer,DealCountUpdater>();
public void update(){
for(Map.Entry<Integer, DealCountUpdater> e:new HashMap<Integer,DealCountUpdater>(dealCountMap).entrySet()){//line:58
System.out.println(e.hashCode());
}
}
当我运行此代码时,出现以下异常:
java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793)
at java.util.HashMap$EntryIterator.next(HashMap.java:834)
at java.util.HashMap$EntryIterator.next(HashMap.java:832)
at java.util.HashMap.putAllForCreate(HashMap.java:435)
at java.util.HashMap.<init>(HashMap.java:225)
at org.my.tuan.count.CountUpdater.update(CountUpdater.java:58)
at org.my.tuan._Maintainer.run(TuanSched.java:110)
这一行是 CountUpdater.java:58 :
for(Map.Entry<Integer, DealCountUpdater> e:new HashMap<Integer,DealCountUpdater>(dealCountMap).entrySet()){
我用谷歌搜索这个程序,我知道我可以使用 ConcurrentHashMap
而不是普通的 HashMap,
但我想知道,为什么我使用 :
new HashMap<Integer,DealCountUpdater>(dealCountMap)
为 HashMap 创建新实例,仍然抛出 ConcurrentModificationException
?
如何通过不使用 ConcurrentHashMap 来修复它?
感谢您的帮助:)
code:
Map<Integer,DealCountUpdater> dealCountMap=new HashMap<Integer,DealCountUpdater>();
public void update(){
for(Map.Entry<Integer, DealCountUpdater> e:new HashMap<Integer,DealCountUpdater>(dealCountMap).entrySet()){//line:58
System.out.println(e.hashCode());
}
}
when I run this code,get below exception:
java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793)
at java.util.HashMap$EntryIterator.next(HashMap.java:834)
at java.util.HashMap$EntryIterator.next(HashMap.java:832)
at java.util.HashMap.putAllForCreate(HashMap.java:435)
at java.util.HashMap.<init>(HashMap.java:225)
at org.my.tuan.count.CountUpdater.update(CountUpdater.java:58)
at org.my.tuan._Maintainer.run(TuanSched.java:110)
this line is CountUpdater.java:58 :
for(Map.Entry<Integer, DealCountUpdater> e:new HashMap<Integer,DealCountUpdater>(dealCountMap).entrySet()){
I google this program,I know I can using a ConcurrentHashMap
instead of a plain HashMap
,
but I want to know ,why I using :
new HashMap<Integer,DealCountUpdater>(dealCountMap)
to create new Instance for HashMap,still throw ConcurrentModificationException
?
how to fix it by not using ConcurrentHashMap
?
thanks for help :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
原因是这样的:
不使用ConcurrentHashMap如何解决?
但我仍然建议使用 ConcurrentHashMap,除非你确实有你的理由。
The reason is this:
How to solve it without using ConcurrentHashMap?
But I would still suggest using ConcurrentHashMap, unless you really have your reasons.
当另一个线程正在迭代它时,您根本不能或不应该在线程中修改哈希图。在这种情况下,HashMap 迭代器将抛出 ConcurrentModificationException。这称为迭代器的“快速失败”行为,java 文档中明确提到了这一点。最适合您的解决方案是使用 ConcurrentHashMap。如果你不想使用那个,那么你必须取消多线程。正如吉姆指出的那样,如果您可以提供有关多线程的更多详细信息,那么您可能会得到更好的解决方案。另外,为什么你不想使用ConcurrentHashMap?有什么特别的原因吗?
You simply cannot,or should not, modify a hashmap in a thread while another thread is iterating over it. In such cases, the HashMap iterator will throw a ConcurrentModificationException. This is called 'fail-fast' behaviour of iterators as it is clearly mentioned in the java docs. The best solution for you is to use ConcurrentHashMap. If you don't want to use that one, then you have to do away with the multi-threading. As jim pointed out, if you can give more details about your multi-threading then you might get a better solution. Also, why do you want to NOT use ConcurrentHashMap? Any particular reason?