我使用多线程在Excel中使用Apache POI包创建行。 每个线程都会创建一行。为了避免并发问题,我将同步块内的代码输出,以确保只有一个线程可以同时创建行和单元格。但是我仍然得到“ConcurrentModificationException”。代码运行时,我可以在代码处理的相同位置重现异常。
the exception is
java.util.ConcurrentModificationException
at java.util.TreeMap$NavigableSubMap$SubMapIterator.nextEntry(TreeMap.java:1703)
at java.util.TreeMap$NavigableSubMap$SubMapEntryIterator.next(TreeMap.java:1751)
at java.util.TreeMap$NavigableSubMap$SubMapEntryIterator.next(TreeMap.java:1745)
at java.util.TreeMap$NavigableSubMap$EntrySetView.size(TreeMap.java:1637)
at java.util.TreeMap$NavigableSubMap.size(TreeMap.java:1507)
at org.apache.poi.xssf.usermodel.XSSFSheet.createRow(XSSFSheet.java:777)
at org.apache.poi.xssf.usermodel.XSSFSheet.createRow(XSSFSheet.java:149)
at com.citi.mrr.rootquery.RootQueryThread.writeQueryResultToRow(RootQueryThread.java:157)
at com.citi.mrr.rootquery.RootQueryThread.run(RootQueryThread.java:42)
at java.lang.Thread.run(Thread.java:748)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
我的代码是:
@Override
public void run() {
...
//add row to excel sheet
synchronized(this){
Row row = sheet.createRow(querySequence+1); // here I get exception
row.createCell(0).setCellValue( rqResult.getRQName());
..
}
}
感谢您的帮助。