1、使用线程池分批次同步处理数据
List<Data> beanList = mapper.getDataList(param);
if(beanList != null && beanList.size()>0){
int totalSize = beanList.size();
int batchSize = 1000;
ExecutorService executor = Executors.newFixedThreadPool(10);
int numBatches = (int) Math.ceil((double) totalSize / batchSize);
for (int i = 0; i < numBatches; i++) {
int startIndex = i * batchSize;
int endIndex = Math.min(startIndex + batchSize, totalSize);
List<Data> batchList = Lists.newArrayList();
for (int j = startIndex; j < endIndex; j++) {
batchList.add(beanList.get(j));
}
// 提交任务给线程池
executor.execute(() -> {
// 执行更新操作
update(batchList);
});
}
// 关闭线程池
executor.shutdown();
}
2、使用java8,parallelStream流
List<Data> list = mapper.listData();
list.parallelStream().forEach(data ->{
update(data)
});