使用多线程的方式批量处理数据操作。示例如下:
@Test
public void syncAccount() throws Exception {
// 每500条数据开启一条线程
int threadSize = 50;
// 总数据条数
int dataSize = readAll.size();
// 方法一:每个线程处理数据量
int threadNum = dataSize / threadSize + 1;
if (dataSize % threadSize == 0) {
threadNum = threadNum - 1;
}
// 方法一:每个线程处理数据量
// int threadNum = (threadSize - 1) / threadSize + 1;
// 初始化线程池
ExecutorService executorService = new ThreadPoolExecutor(threadNum,// 核心线程池大小
100,// 线程池最大容量大小
0L,// 线程池空闲时,线程存活的时间
TimeUnit.MILLISECONDS,// 时间单位
new linkedBlockingQueue()// 任务队列
);
List>>> tasks = this.multiThread(threadSize, threadNum, dataSize, readAll);
List>>> results = executorService.invokeAll(tasks);
for (Future>> future : results) {
//每个线程返回的数据处理,有顺序对应关系
// System.out.println(future.get());
writeList.addAll(future.get());
}
// 关闭线程池
executorService.shutdown();
System.out.println("线程任务执行结束");
}
private List>>> multiThread(int threadSize, int threadNum, int dataSize, List
—————————
如有不足请留言指正
相互学习,共同进步



