请给我一个List集合使用多线程的方式分批插入数据的Java代码
时间: 2023-03-13 22:06:08 浏览: 117
若依管理存在任何文件读取漏洞检测系统,渗透测试.zip
可以使用如下的Java代码来实现:List list = new ArrayList();
int maxThreadCount = 10;
ExecutorService executorService = Executors.newFixedThreadPool(maxThreadCount);
int batchSize = 1000;
int listSize = list.size();
int threadCount = listSize/batchSize + (listSize%batchSize > 0 ? 1 : 0);
for(int i=0; i<threadCount; i++){
int startIndex = i * batchSize;
int endIndex = (i+1) * batchSize;
endIndex = endIndex > listSize ? listSize : endIndex;
Runnable runnable = new BatchInsertRunnable(list,startIndex, endIndex);
executorService.execute(runnable);
}
executorService.shutdown();
while(!executorService.isTerminated()){
//wait for all thread to finish
}
阅读全文