Java List 数据分批异步执行
1.创建线程池
import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; @Slf4j @Configuration @EnableScheduling // 启用Spring调度功能 public class ThreadPoolTaskSchedulerConfig { @Bean public ThreadPoolTaskScheduler taskScheduler() { ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); // 核心配置 scheduler.setPoolSize(32); // 线程池大小 scheduler.setThreadNamePrefix("task-scheduler-"); // 线程名前缀 scheduler.setAwaitTerminationSeconds(60); // 关闭时等待任务完成的时间 scheduler.setWaitForTasksToCompleteOnShutdown(true); // 是否等待计划任务完成 scheduler.setRemoveOnCancelPolicy(true); // 取消任务时立即删除 // 线程配置 scheduler.setDaemon(false); // 是否守护线程 scheduler.setThreadPriority(Thread.NORM_PRIORITY); // 线程优先级 // 错误处理 scheduler.setErrorHandler(throwable -> { log.error("Task execution error", throwable); }); scheduler.initialize(); // 初始化 return scheduler; } }
2.分批异步执行
@Autowired
private ThreadPoolTaskScheduler taskScheduler;
public void process() {
List<Person> personList = personService.findAll();
List<List<Person>> batches = new ArrayList<>();
List<CompletableFuture<Void>> futures = new ArrayList<>();
batches = ListUtils.partition(personList, 1000);
futures = batches.stream()
.map(batch -> CompletableFuture.runAsync(() -> {
// 分批执行
processSubPersonList(batch);
}, taskScheduler))
.collect(Collectors.toList());
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
}

浙公网安备 33010602011771号