这是我的服务代码,会引发并发修改:
public List<PriceTierMI> getMutablePriceTiers() {
if (mutablePriceList == null) {
mutablePriceList = new ArrayList<PriceTierMI>();
} else {
if (mutablePriceList.size() > 0) {
Collections.sort(mutablePriceList , tierComparator);
}
}
return new ArrayList<>(mutablePriceList);
}
public List<PriceTierII> getImmutablePriceTiers() {
return new ArrayList<>(getMutablePriceTiers());
}
**Exception:**
java.util.ConcurrentModificationException
at java.util.ArrayList.sort(ArrayList.java:1473)
at java.util.Collections.sort(Collections.java:186)
at ItemPriceTierSequence.getMutablePriceTiers(ItemPriceTierSequence.java:196)
at ItemPriceTierSequence.getImmutablePriceTiers(ItemPriceTierSequence.java:207)
通过初始调试,我发现它不必要地进行了2次排序。但是,在删除getImmutablePriceTiers()中的排序之后,我仍然遇到并发修改异常。我不确定代码中还缺少什么?解决该问题的任何指示都将有所帮助。