Java中的ConcurrentModificationException是什么?
例子
让我们看一个例子来说明这个异常是如何发生的。import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
//Test driver for ConcurrentModificationException
public class ConcurrentModificationExceptionTestDriver1 {
public static void main(String[] args) {
List<String> businessDays = new ArrayList<>();
businessDays.add("Monday");
businessDays.add("Tuesday");
businessDays.add("Wednesday");
businessDays.add("Thursday");
businessDays.add("Friday");
businessDays.add("Saturday");
businessDays.add("Sunday");
Iterator<String> iterator = businessDays.iterator();
while (iterator.hasNext()) {
String day = iterator.next();
if (day.equals("Sunday")) {
// removing a list item while iterating
// leading to java.util.ConcurrentModificationException
businessDays.remove(day);
}
}
}
}
输出
线程“main”中的异常 java.util.ConcurrentModificationException 在 java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1042) 在 java.base/java.util.ArrayList$Itr.next(ArrayList.java :996)在milestone.thirty.seven.ConcurrentModificationExceptionTestDriver1.main(ConcurrentModificationExceptionTestDriver1.java:22)
Java中如何避免ConcurrentModificationException?
ConcurrentModificationException有不同的解决方法。这里列出了一些。您可以根据您的需要选择任何人。- 避免迭代时修改。
- 使用传统循环进行同时迭代和修改。
- 创建要在使用迭代器时修改的值的副本。迭代结束后更新值。
例子
以下是如何避免ConcurrentModificationException的示例。import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
//Test driver for ConcurrentModificationException
public class ConcurrentModificationExceptionTestDriver2 {
public static void main(String[] args) {
List<String> businessDays = new ArrayList<>();
businessDays.add("Monday");
businessDays.add("Tuesday");
businessDays.add("Wednesday");
businessDays.add("Thursday");
businessDays.add("Friday");
businessDays.add("Saturday");
businessDays.add("Sunday");
System.out.println("Initial Business Days: " + businessDays);
// Method II - Using traditional for loop
for (int i = 0; i < businessDays.size(); i++) {
if (businessDays.get(i).equals("Sunday")) {
businessDays.remove(i);
}
}
Iterator<String> iterator = businessDays.iterator();
List<String> businessDaysToBeRemoved = new ArrayList<>();
// Method III - Creating a copy of elements
while (iterator.hasNext()) {
String day = iterator.next();
if (day.equals("Saturday")) {
// Add values to be removed in the list
businessDaysToBeRemoved.add(day);
}
}
businessDays.removeAll(businessDaysToBeRemoved);
System.out.println("Final Business Days: " + businessDays);
}
}
输出
初始工作日:[周一、周二、周三、周四、周五、周六、周日] 最终工作日:[周一、周二、周三、周四、周五]
GO TO FULL VERSION