考慮下面的代碼:併發修改異常。在這種情況下如何避免?
List tableData = null;
tableData = new ArrayList(records.size());
for (Iterator iter = records.iterator(); iter.hasNext();) {
Test record = (Test) iter.next();
Map rowData = createRowData(record);
if (rowData != null) {
// sorted insert
Date newDate = (Date) rowData.get(TestModel.TIMESTAMP);
boolean done = false;
for (int row = 0; row < tableData.size(); row++) {
Map currentRow = (Map) tableData.get(row);
Date currentDate = (Date) currentRow.get(TestModel.TIMESTAMP);
if (currentDate.after(newDate)) {
tableData.add(row, rowData);
done = true;
break;
}
}
if (!done) {
tableData.add(rowData);
}
}
}
} catch(Exception e) {
throw new RuntimeException("Error reading fuel and SMU data", e);
}
return tableData;
records
這裏有new Vector()
正在被初始化其間。
由於ArrayList具有int容量的參數。在這種情況下如何使用CopyOnWriteArrayList
?
`STACKTRACE:
at com.mincom.explorer.uif.AbstractRequestHandler.handleException(AbstractRequestHandler.java:255)
at com.mincom.explorer.uif.AbstractRequestHandler.readData(AbstractRequestHandler.java:137)
at com.mincom.jive.service.RequestExecutor$RequestHandlerCommand.mainProcess(RequestExecutor.java:156)
at com.mincom.util.executor.AbstractCommand.run(AbstractCommand.java:56)
at com.mincom.util.executor.SimpleExecutor$WorkerThread.run(SimpleExecutor.java:156)
THROWABLE: java.lang.RuntimeException: Error reading fuel and SMU data
at minestar.production.presentation.page.fuel.smu.assistant.*.getEventsUsingFilter(*.java:243)
at minestar.production.presentation.page.fuel.smu.assistant.*.processReadData(*.java:174)
at com.mincom.explorer.uif.AbstractRequestHandler.readData(AbstractRequestHandler.java:129)
at com.mincom.jive.service.RequestExecutor$RequestHandlerCommand.mainProcess(RequestExecutor.java:156)
at com.mincom.util.executor.AbstractCommand.run(AbstractCommand.java:56)
at com.mincom.util.executor.SimpleExecutor$WorkerThread.run(SimpleExecutor.java:156)
Caused by: java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at *.production.presentation.page.fuel.smu.assistant.*.getEventsUsingFilter(*.java:222)
... 5 more`
我已經修改了這段代碼。請看一看。
從解釋到底是什麼引發異常開始。 – 2015-02-11 13:31:07
這個問題完全含糊不清。請提供**完整的**示例源代碼以及** ConcurrentModificationException的完整**異常堆棧跟蹤,我們可以嘗試並複製這些代碼以便準確地幫助您。 – 2015-02-11 13:31:09
@MarkoTopolnik我編輯了我的問題。 – NewBie 2015-02-11 13:37:32