简体   繁体   English

遍历列表但未修改列表时发生ConcurrentModificationException

[英]ConcurrentModificationException while iterating through a list but not modifying it

I am running my application for load testing using JMeter and getting ConcurrentModificationException in a scenario where (I think) its not supposed to come. 我正在使用JMeter运行我的应用程序以进行负载测试,并在(我认为)它不应该出现的情况下获得ConcurrentModificationException This occurred only for few cases and not all. 这种情况仅发生在少数情况下,并非全部发生。

I am having an ArrayList which is a class variable in which elements are added through a method. 我有一个ArrayList ,它是一个类变量,通过方法在其中添加元素。 Later on in a different method which is invoked after the list is prepared, iteration is going on using stream and a local arraylist is getting prepared using some filters as follows 稍后,在准备列表之后调用另一种方法,使用stream继续进行迭代,并使用一些过滤器准备本地数组列表,如下所示

...
//class variable getting elements from a separate method
List<String> myGlobalList = new ArrayList(); 
...

public String returnValue(String _value){
  List myLocalList = new ArrayList();
  myGlobalList.stream().filter(ele -> {
    ele = ele.replaceAll("\\[(.*?)\\]", "$1");

    String[] strArr = ele.split(",");
    for(String sArr : strArr) {
      if(sArr.contains(_value)){
        System.out.println("sArr ---- > "+sArr);
        myLocalList.add(sArr);
        return true ;
      }
    }
    return false;
  }).findFirst();

  return myLocalList.size()>0 ? myLocalList.get(0): null;
}

The findFirst() method is throwing ConcurrentModificationException only under load (not for every request). findFirst()方法仅在负载下抛出ConcurrentModificationException (并非针对每个请求)。

Can't figure out why it's happening. 无法弄清楚为什么会这样。 Am I missing any key? 我缺少任何钥匙吗? How can this code be modified to avoid this exception in all cases? 在所有情况下如何修改此代码以避免此异常?

Update: 更新:

Below is a snippet from the stacktrace for the exception 下面是来自堆栈跟踪的摘录

java.util.ConcurrentModificationException
at java.util.ArrayList$ArrayListSpliterator.tryAdvance(ArrayList.java:1353)
at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:498)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:485)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:152)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:464)
at com.pkg.MyClass.returnValue(MyClass.java:501)

Don't you need to get the first String in list? 您是否不需要获取列表中的第一个字符串? why not get it without using List: 为什么不使用List就不获取它:

  public String returnValue(String _value){   
  String findFirst = myGlobalList.stream().filter(ele -> {
    ele = ele.replaceAll("\\[(.*?)\\]", "$1");

    String[] strArr = ele.split(",");
    for(String sArr : strArr) {
      if(sArr.contains(_value)){
        System.out.println("sArr ---- > "+sArr);
        //myLocalList.add(sArr);
        return true ;
      }
    }
    return false;
  }).findFirst().get();

  return findFirst;
}

EDIT 编辑

With Optional check: 带有可选检查:

        Optional<String> findFirst = myGlobalList.stream().filter(ele -> {
        ele = ele.replaceAll("\\[(.*?)\\]", "$1");

        String[] strArr = ele.split(",");
        for (String sArr : strArr) {
            if (sArr.contains(_value)) {
                System.out.println("sArr ---- > " + sArr);
                // myLocalList.add(sArr);
                return true;
            }
        }
        return false;
    }).findFirst();
    return findFirst.isPresent() ? findFirst.get() : null;

EDIT 2 编辑2

findFirst is throwing the exception, because original list have been changed, try collecting your results to new list using collect and then get the first value: findFirst异常,因为原始列表已更改,请尝试使用collect将结果收集到新列表中,然后获取第一个值:

 return myGlobalList.stream().filter(ele -> {
        ele = ele.replaceAll("\\[(.*?)\\]", "$1");

        String[] strArr = ele.split(",");
        for (String sArr : strArr) {
            if (sArr.contains(_value)) {
                //System.out.println("sArr ---- > " + sArr);
                // myLocalList.add(sArr);
                return true;
            }
        }
        return false;
    }).collect(Collectors.toList()).get(0);

暂无
暂无

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

相关问题 遍历List时发生ConcurrentModificationException,尽管未对其进行任何修改 - ConcurrentModificationException while iterating through List, altough not modifying it 通过HashMap迭代时出现ConcurrentModificationException - ConcurrentModificationException while iterating through HashMap 迭代Arraylist时的ConcurrentModificationException(不删除) - ConcurrentModificationException while iterating through Arraylist (not removing) 在遍历元素时删除元素。 removeIf 导致 ConcurrentModificationException - Removing element while iterating through it. removeIf result in ConcurrentModificationException Java ConcurrentModificationException:是否可以在迭代时向哈希表添加元素? - Java ConcurrentModificationException: Is it possible to add elements to a hashtable while iterating through it? 迭代列表时遇到java.util.ConcurrentModificationException - Running into java.util.ConcurrentModificationException while iterating list 为什么我在同时迭代和修改HashMap时没有得到“ConcurrentModificationException”? - How come I am not getting “ConcurrentModificationException” while iterating and modifying on HashMap at the same time? 不修改列表但仍然得到ConcurrentModificationException - Not modifying list but still get a ConcurrentModificationException 迭代ArrayList时出现ConcurrentModificationException <Integer> - ConcurrentModificationException when iterating through an ArrayList <Integer> 遍历集合以添加项目但抛出 ConcurrentModificationException - Iterating through collections to add items but throwing ConcurrentModificationException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM