List list=new ArrayList()抛出的ArrayIndexOutOfBoundsException异常

1.应用场景,今天生产日志监控到一组new ArrayList() 进行add 异常,具体日志如下:

java 复制代码
eptionHandler.handler(178): TXXYBUSSINESS|执行异常
java.util.concurrent.CompletionException: java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 0
	at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:315) ~[?:?]
	at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:320) ~[?:?]
	at java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1807) ~[?:?]
	at com.txxy.common.support.ThreadContextTaskDecorator.lambda$decorate$0(ThreadContextTaskDecorator.java:20) ~[txxy-common.jar:10.3.10-SNAPSHOT]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) ~[?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) ~[?:?]
	at java.lang.Thread.run(Thread.java:833) ~[?:?]
Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 0
	at java.util.ArrayList.add(ArrayList.java:455) ~[?:?]
	at java.util.ArrayList.add(ArrayList.java:467) ~[?:?]
	at com.txxy.api.support.PersonalRiskReportSupport.lambda$riskType4$28(PersonalRiskReportSupport.java:528) ~[classes/:?]
	at java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1804) ~[?:?]

具体代码:

java 复制代码
List<DTO> riskSummaryList = new ArrayList<>();
                        // 10条记录批量查询
                        Lists.partition(ids, Paging.DEF_PAGE_SIZE).forEach(arrIds -> {
                            List<CompletableFuture<Void>> list =
                                arrIds.stream().map(id -> CompletableFuture.runAsync(() -> {
                                    try {
                                     // ....代码省略了http请求 得到的responseDetail 结果集
                                       
                                        if (responseDetail.isSuccess()) {
                                            riskSummaryList.add(new DTO() );
                                        }
                                    } catch (TxxyBusinessException e) {
                                      
                                    }
                                }, apiTaskExecutor)).collect(Collectors.toList());
                            list.stream().map(CompletableFuture::join).collect(Collectors.toList());

                        });

                        result.setRiskSummaryList(riskSummaryList);

上面代码是对ids 分片按照10条记录查询远程接口,并将结果集放到list riskSummaryList 对象中 riskSummaryList.add(new DTO() )最后输出结果,代码比较简单.

分析:这种情况属于多线程并发产生的,ArrayList是一个非线程安全对象,在并发操作同一个对象时就可能出现上面的异常 情况,ArrayList 源码中ensureCapacity 这个方法是非线程安全的导致计算值扩容不够抛出的异常。

查看ArrayList 源码:

解决方法:

1.使用官网提供的方法Collections.synchronizedList

java 复制代码
  List list = Collections.synchronizedList(new ArrayList(...));

2.new一个对象CopyOnWriteArrayList方法

java 复制代码
new CopyOnWriteArrayList<>()

最后希望能帮助到你解决问题,或是评论区留言问题进行讨论~

相关推荐
web3.08889998 小时前
微店商品详情API实用
python·json·时序数据库
知乎的哥廷根数学学派9 小时前
基于数据驱动的自适应正交小波基优化算法(Python)
开发语言·网络·人工智能·pytorch·python·深度学习·算法
sunfove9 小时前
将 Python 仿真工具部署并嵌入个人博客
开发语言·数据库·python
Learner9 小时前
Python类
开发语言·python
2501_941329729 小时前
门及其组件定位识别_YOLO13-C3k2-PoolingFormer改进模型研究
python
Ancelin安心9 小时前
kali-dirsearch的使用
linux·运维·服务器·python·计算机网络·web安全·网络安全
努力学习的小洋9 小时前
Python训练打卡Day5离散特征的处理-独热编码
人工智能·python·机器学习
Sherry Wangs10 小时前
【ML】机器学习进阶
人工智能·python·机器学习
X1A0RAN10 小时前
python 借助 paramiko 库执行 SSH命令报错:input is not a terminal 解决方式
开发语言·python·ssh
冰清-小魔鱼10 小时前
各类数据存储结构总结
开发语言·数据结构·数据库