java.util.ConcurrentModificationException

出现异常的位置在After方法中遍历集合的那行

//在测试单元中有如下代码


//遍历Future载体
        Thread thread=new Thread(()->{
        listFuture.forEach(x->{
            try {
                if (x.isDone()) {
                    System.out.println("future:"+x+",result:"+x.get());
                }
                list.add(Long.toString(System.currentTimeMillis()));
            } catch (Exception e) {
                System.out.println("抛出异常:"+e);
            }
        });
      });
      thread.start();

//异常出现的位置在After单元中遍历集合一行

@After
    public void clear(){
        System.out.println("遍历list集合");
        list.forEach(System.out::println);
    }

官方说明:

问题出在遍历集合时thread线程并未执行完

解决方案:

保证子线程在主线程结束之前执行完毕。

重新测试后能用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值