一.参考
1.Java Essentials: Preventing ConcurrentModificationException
https://www.codebyamir.com/blog/java-essentials-preventing-concurrentmodificationexception
二.
Overview
The JVM will throw a ConcurrentModificationException at runtime whenever we try to modify a collection while iterating over it.
In this article, we’ll explain why this happens and some solutions to prevent this.
Example
Suppose we populate a list with names and then later want to remove some names from the list.
Code
We may write something like this:
List names = new ArrayList<>();
names.add(“Amir”);
names.add(“Beth”);
names.add(“Arnie”);
names.add(“Lucy”);
for (String name : names) {
if (name.startsWith(“A”)) {
names.remove(name);
}
}
System.out.println(names);
Output
This code will compile successfully but throw an exception at runtime on line 10:
Exception in thread “main” java.util.ConcurrentModificationException
at java.util.ArrayList
I
t
r
.
c
h
e
c
k
F
o
r
C
o
m
o
d
i
f
i
c
a
t
i
o
n
(
U
n
k
n
o
w
n
S
o
u
r
c
e
)
a
t
j
a
v
a
.
u
t
i
l
.
A
r
r
a
y
L
i
s
t
Itr.checkForComodification(Unknown Source) at java.util.ArrayList
Itr.checkForComodification(UnknownSource)atjava.util.ArrayListItr.next(Unknown Source)
at com.codebyamir.demo.Main.main(Main.java:10)
Notice that this may only happen intermittently because we are calling remove() inside a conditional statement. So the exception will be thrown whenever we have a String that starts with “A” in our list.
Solutions
There are a number of ways to prevent ConcurrentModificationException, and we’ll explore these below.
Use an Iterator
We can change how we iterate by replacing the enhanced for-loop with a while loop that uses an Iterator object. The Iterator allows us to safely remove the matching element because we are not calling remove() directly on the list object.
Code
List names = new ArrayList<>();
names.add(“Amir”);
names.add(“Beth”);
names.add(“Arnie”);
names.add(“Lucy”);
Iterator iter = names.iterator();
while (iter.hasNext()) {
String name = iter.next();
if (name.startsWith(“A”)) {
iter.remove();
}
}
System.out.println(names);
Output
[Beth, Lucy]
Populate a separate list to keep track of the items to be removed
This approach avoids having to introduce an Iterator object, but it requires another list to keep track of the names we want to remove.
Code
List names = new ArrayList<>();
names.add(“Amir”);
names.add(“Beth”);
names.add(“Arnie”);
names.add(“Lucy”);
List removeNames = new ArrayList<>();
for (String name : names) {
if (name.startsWith(“A”)) {
removeNames.add(name);
}
}
names.removeAll(removeNames);
Output
[Beth, Lucy]
Use Java 8’s removeIf() method
Java 8 added the removeIf() method to the java.util.Collection class.
JDK Code
Let’s take a look at the JDK code for this method. Notice how it uses an Iterator under the hood.
public boolean removeIf(Predicate paramPredicate) {
Objects.requireNonNull(paramPredicate);
boolean bool = false;
Iterator localIterator = iterator();
while (localIterator.hasNext()) {
if (paramPredicate.test(localIterator.next())) {
localIterator.remove();
bool = true;
}
}
return bool;
}
Code
This method makes our code more concise since we can use a Lambda expression.
List names = new ArrayList<>();
names.add(“Amir”);
names.add(“Beth”);
names.add(“Arnie”);
names.add(“Lucy”);
names.removeIf(name -> (name.startsWith(“A”)));
Output
[Beth, Lucy]
Note that ArrayList has an optimized implementation of `removeIf`` (http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/util/ArrayList.java/#1393) which makes it the fastest solution.