映射表头中的ConcurrentModificationException [英] map ConcurrentModificationException in for header

查看:167
本文介绍了映射表头中的ConcurrentModificationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

  private Multimap< Object,ComplexCalcStrategy> strategies = HashMultimap.create(); 
....
集合< ComplexCalcStrategy> strategiesThatNeedUpdating = strategies.get(mktDataChange.getOptionId());
for(ComplexCalcStrategy strategy:strategiesThatNeedUpdating){//行号88
updateMarketData(strategy.getStrategy(),mktDataChange);
}

在日志中我看到以下跟踪:

  java.util.ConcurrentModificationException 
java.util.HashMap $ HashIterator.nextEntry(HashMap.java:793)
在java.util .HashMap $ KeyIterator.next(HashMap.java:828)
在com.google.common.collect.AbstractMapBasedMultimap $ WrappedCollection $ WrappedIterator.next(AbstractMapBasedMultimap.java:486)
在package.ClassName.processMarketDataChange (ClassName.java:88)

我不明白这个代码如何产生CME。 / p>

请分享您的想法。



PS



地图填写在这里:

  @Override 
public void registerStrategy(final ComplexCalcStrategy strategy){
for整数optionId:strategy.getOptions()){
strategies.put(optionId,strategy);

}
if(strategy.getStrategy().trustToStock){
strategies.put(strategy.getUnderlying(),strategy);
}

....
}

这个方法在另一个线程中调用,看起来像某个时候的迭代,映射填充同时发生,这是根本原因。



我明白我可以使用同步版本的multimap。 / p>

是否会修复问题?



有更好的方法吗?

解决方案

ConcurrentModificationException


此异常可能会被检测到对对象进行并发修改的方法抛出,这种修改不允许。


如果您在迭代期间修改 Multimap 中的键/值键/值然后你不能继续迭代修改(因此例外)。



考虑使用 Multimaps.transformValues(Multimap,Function)


I have the following code:

private Multimap<Object, ComplexCalcStrategy> strategies = HashMultimap.create();
....
Collection<ComplexCalcStrategy> strategiesThatNeedUpdating = strategies.get(mktDataChange.getOptionId());
for (ComplexCalcStrategy strategy : strategiesThatNeedUpdating) {  //row number 88
     updateMarketData(strategy.getStrategy(), mktDataChange);
}

and in logs I see following trace:

java.util.ConcurrentModificationException
    at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793)
    at java.util.HashMap$KeyIterator.next(HashMap.java:828)
    at com.google.common.collect.AbstractMapBasedMultimap$WrappedCollection$WrappedIterator.next(AbstractMapBasedMultimap.java:486)
    at package.ClassName.processMarketDataChange(ClassName.java:88)

I don't understand how does this code can produce CME.

Please, share your ideas.

P.S.

map fills here:

@Override
public void registerStrategy(final ComplexCalcStrategy strategy) {
    for (Integer optionId : strategy.getOptions()) {
        strategies.put(optionId, strategy);

    }
    if (strategy.getStrategy().tiedToStock) {
        strategies.put(strategy.getUnderlying(), strategy);
    }

    ....
}

This method invokes in another thread and looks like sometime iteration and map filling happens simultaneously and it is root cause.

I understand that I can use synchronized version of multimap.

Will it fix issue?

Is there nicer way?

解决方案

ConcurrentModificationException:

This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.

If you modify the keys/values in a Multimap during iteration of the keys/values then you cannot continue iterating after the modification (hence the exception).

Consider using Multimaps.transformValues(Multimap, Function) instead.

这篇关于映射表头中的ConcurrentModificationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆