我不知道为什么在遍历ArrayList时会出现ConcurrentModificationException异常。这个ArrayList是方法作用域的,因此其他执行相同代码的线程不应该能够看到它。至少,如果我正确理解多线程和变量作用域的话。
Caused by: java.util.ConcurrentModificationException
at java.util.AbstractList$SimpleListIterator.next(AbstractList.java:64)
at com....StrategyHandler.applyStrategy(StrategyHandler.java:184)
private List<Order> applyStrategy(StorageObjectTree storageObjectTree) {
...
List<OrderHeader> finalList = new ArrayList<Order>();
for (StorageObject storageObject : storageObjectTree.getStorageObjects()) {
List<Order> currentOrders = strategy.process(storageObject);
...
if (currentOrders != null) {
Iterator<Order> iterator = currentOrders.iterator();
while (iterator.hasNext()) {
Order order = (Order) iterator.next(); // line 64
// read some values from order
}
finalList.addAll(currentOrders);
}
}
return finalList;
}
有人能给我一个提示,问题的根源可能是什么吗?
List<Order> currentOrders = new ArrayList<>(strategy.process(storageObject));
- pillingworth