当前位置: 首页 > 知识库问答 >
问题:

使用java.util.concurrentModificationException运行

孔征
2023-03-14

我试着用Java8编写代码。我用IDEA运行它。当我在云服务器上开发它时,它遇到了以下异常:

java.util.ConcurrentModificationException
    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1380)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:512)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:502)
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
List<Data> validDOList = DataDOList.stream()
                .filter(Objects::nonNull)
                .filter(dataDO -> dataDO.getId() % 2 == 0)
                .collect(Collectors.toList());

-------更新-------------------------------------------------------------------

List<SkuPriceDataDO> validSkuPriceDOList = skuPriceDataDOList.stream()
                    .filter(Objects::nonNull)
                    .filter(skuPriceDataDO -> isValidSkuPriceDataDO(skuRealTimeDataDoMap.get(ESUtils
                            .generateDocId(skuPriceDataDO.getPoiId(),
                                    skuPriceDataDO.getSkuId(), skuPriceDataDO.getScheduleDate())), skuPriceDataDO))
                    .collect(Collectors.toList());

筛选方法详细信息

    private boolean isValidSkuPriceDataDO(SkuRealTimeDataDO skuRealTimeDataDO, SkuPriceDataDO skuPriceDataDO) {
        if (skuRealTimeDataDO == null || skuPriceDataDO == null) {
            return false;
        }
        boolean equal = StringUtils.equals(skuPriceDataDO.getSpecUnit(), skuRealTimeDataDO.getSpecUnit())
                && Objects.equals(skuPriceDataDO.getSpecValue(), skuRealTimeDataDO.getSpecValue())
                && Objects.equals(skuPriceDataDO.getAvgPrice7dWholesale(), skuRealTimeDataDO.getAvgPrice7dWholesale())
                && Objects.equals(skuPriceDataDO.getAvgPrice1dWholesale(), skuRealTimeDataDO.getAvgPrice1dWholesale())
                && Objects.equals(skuPriceDataDO.getAvgPrice1dCx(), skuRealTimeDataDO.getAvgPrice1dCx())
                && Objects.equals(skuPriceDataDO.getAvgPrice7dCx(), skuRealTimeDataDO.getAvgPrice7dCx())
                && Objects.equals(skuPriceDataDO.getAvgPrice1dDd(), skuRealTimeDataDO.getAvgPrice1dDd())
                && Objects.equals(skuPriceDataDO.getAvgPrice7dDd(), skuRealTimeDataDO.getAvgPrice7dDd());
        return !equal;
    }

共有1个答案

吕向荣
2023-03-14

某些线程试图访问和mpdify列表导致的错误,这不是并发的或线程安全的,导致内存修改数据发生冲突。

您应该使用一些线程安全的列表类型,如CopyOnWriteArrayListLinkedBlockingQueue。使用CopyOnWriteArrayList而不是List

 类似资料: