java8 stream map reduce导致一个ConcurrentModificationException,为什么?

时间:2016-08-04 06:03:14

标签: java java-8 java-stream concurrentmodification

updates.map(f =>(null,f)).saveAsNewAPIHadoopFile(
      "file:///this-is-completely-unused",
      classOf[Object],
      classOf[MongoUpdateWritable],
      classOf[MongoOutputFormat[Object, MongoUpdateWritable]],
      outputConfig)

导致final BigDecimal couponlessAmount = orderItems.stream() .filter(item -> !item.getIsUseCoupon()) .map(item -> item.getTotalAmount().subtract(item.getReduceProductAmount())) .reduce(BigDecimal.ZERO, BigDecimal::add);

的原因
java.util.ConcurrentModificationException

1 个答案:

答案 0 :(得分:0)

它不太可能是JDK / JIT错误。与orderItems一样,它表示您的ArrayList是从另一个线程同时修改的。如果不提供适当的外部同步,则不应同时使用ArrayList。请注意,将Collections.synchronizedList包装到<div class="btn-group" data-toggle="buttons"> <label class="btn btn-default active"> <input type="radio" name="payment_options" value=1 autocomplete="off" checked>Cash on Delivery </label> <label class="btn btn-default" id="bkash-radio"> <input type="radio" name="payment_options" value=2 autocomplete="off">bKash </label> </div> <input type="hidden" name="trx_id" id="trx_id" class="form-control" placeholder="Enter Transaction ID"/> 中是不够的,因为它不会同步遍历。作为替代方案(如果修改很少),请使用CopyOnWriteArrayList