gpt4 book ai didi

ArrayList处理中的java.util.ConcurrentModificationException

转载 作者:行者123 更新时间:2023-12-01 23:24:38 25 4
gpt4 key购买 nike

java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343)
at com.alpha.beta.purchasing.item.VendorItemList.loadItems(VendorItemList.java:51)
at com.alpha.beta.purchasing.Shipment.loadItems(Shipment.java:1006)
at com.alpha.beta.purchasing.Shipment.loadItems(Shipment.java:953)
at com.alpha.beta.purchasing.Shipment.getItemTotal(Shipment.java:1503)
at com.alpha.beta.purchasing.Shipment.getShipmentTotal(Shipment.java:854)
at com.alpha.beta.quickreports.PurchasingGenericListSourceMapper.fillPurchaseReceivingItemListForQuickReportsTask(PurchasingGenericListSourceMapper.java:144)
at com.alpha.beta.quickreports.PurchasingGenericListSourceMapper$2.run(PurchasingGenericListSourceMapper.java:105)
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)

我收到上述异常,源代码如下:

public void loadItems(ArrayList list) {
if(list != null && list.size() > 0) {
super.clear();
Iterator iterator = list.iterator();
while(iterator.hasNext()) {
// VendorItem item = (VendorItem)iterator.next();
//load(new Integer(item.getVendorItemId()), item);

Object obj = iterator.next(); // << This is where it says exception comes
if(obj instanceof InvoiceItem) {
InvoiceItem item = (InvoiceItem)obj;
load(new Integer(item.getInvoiceItemId()), item);
//logger.debug("** loading invoice Item " + item.toString());
}
else if(obj instanceof PurchaseOrderItem) {
PurchaseOrderItem item = (PurchaseOrderItem)obj;
load(new Integer(item.getPoItemId()), item);
//logger.debug("** loading PO Item " + item.toString());
}
else if(obj instanceof ShipmentItem) {
ShipmentItem item = (ShipmentItem)obj;
load(new Integer(item.getShipmentItemId()), item);
logger.debug("** loading ShipmentItem Item " + item.toString());
}
//
else if(obj instanceof VendorItem) {
VendorItem item = (VendorItem)obj;
load(new Integer(item.getVendorItemId()), item);
logger.debug("** loading VendorItem " + item.toString());
}
//
else {
logger.debug("*** neither invoice/PO/shipment item");
}
}
}

}

我已经看到了相关问题,但它们不符合我的情况,所以我希望有人能指出发生这种情况的真正原因。

最佳答案

ArrayList 未同步。这意味着在您的 TimerThread 中还有其他东西正在修改 ArrayList。

这是文档所说的:

Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list. If no such object exists, the list should be "wrapped" using the Collections.synchronizedList method. This is best done at creation time, to prevent accidental unsynchronized access to the list:

   List list = Collections.synchronizedList(new ArrayList(...));

使用 Collections.synchronizedList() 包装此列表应该可以解决问题。

关于ArrayList处理中的java.util.ConcurrentModificationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20199760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com