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<>()

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

相关推荐
XMYX-013 分钟前
Python 实现一个带进度条的 URL 批量下载工具(含 GUI 界面)
开发语言·python
未来并未来1 小时前
Sentinel 流量控制安装与使用
开发语言·python·sentinel
东皇太星2 小时前
Python 100个常用函数全面解析
开发语言·python
luofeiju3 小时前
数字图像处理与OpenCV初探
c++·图像处理·python·opencv·计算机视觉
壹米饭3 小时前
Java程序员学Python学习笔记一:学习python的动机与思考
java·后端·python
电院工程师3 小时前
SM3算法Python实现(无第三方库)
开发语言·python·算法·安全·密码学
CodeDevMaster4 小时前
在Jupyter Notebook中使用Conda虚拟环境
python·jupyter
冷月半明4 小时前
告别手动拖动!Python+dddocr自动化破解多缺口滑块
python
Kusunoki_D4 小时前
Python 实现 Web 静态服务器(HTTP 协议)
服务器·前端·python
站大爷IP4 小时前
当Python遇上多线程:ThreadPoolExecutor的实用指南
python