当任务需要处理大量数据时,防止内存溢出应多次分批处理数据
例如当使用spring定时任务时
//定时任务规则
@Scheduled(cron = "30 * * * * ?")
public void test() {
//分页index
//pageSize
Integer pageIndex = 0;
while (true) {
//DAO层查询数据库
List<UserDto> userList = userDAO.getUser(pageIndex(pageIndex)*num,num, pageSize);
if (userList == null || userList .size() == 0) {
break;
}
if (CollectionUtils.isNotEmpty(userList )) {
accountList.forEach(
account -> {
try {
//doSomething
} catch (Exception e) {
}
});
++pageIndex;
}
}
}