I am running my application for load testing using JMeter and getting ConcurrentModificationException
in a scenario where (I think) its not supposed to come. 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. 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
...
//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).
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)