在进行接口之间数据同步时候,数据量大的话,定时任务或者接口会执行很久,需要优化的话,思路可从以下几点考虑:
1.先分页处理。
2.分页中进行多线程处理。
3.根据服务器配置,决定开启的多线程条数,一般的服务器配置建议开启10-15个。
代码逻辑参考如下:
//数据总条数
int total = xxxxxx.selectCount();
//共查询多少次
int count = 0;
if (total > pageSize) {
count = total / pageSize + 1;
} else {
count = 1;
}
//分页查询处理
for (int i = 0; i < count; i++) {
//分页查询数据
List<XXX> list = xxxx.selectPageList(i * pageSize, pageSize);
//开启线程
if (CollectionUtil.isNotEmpty(list)) {
//默认开启10个线程
int nThreads = 10;
ExecutorService executorService = newFixedThreadPool(nThreads);
List<Future<Integer>> futures = new ArrayList<Future<Integer>>(nThreads);
int size = list.size();
for (int a = 0; a < nThreads; a++) {
final List<RmcpAcUserCp> list = list.subList(size / nThreads * i, size / nThreads * (i + 1));
Callable<Integer> task1 = () -> {
//业务逻辑处理
this.syncIscPropertyInfo(list);
return 1;
};
futures.add(executorService.submit(task1));
}
executorService.shutdown();
}
}
如有更好更合适的,欢迎大家贴代码共享讨论。