@ConfigurationProperties(prefix = "xxx.thread")
@Data
public class ThreadPoolConfig {
private Integer coreSize;
private Integer maxSize;
private Integer keepAliveTime;
private Integer queueSize;
}
@EnableConfigurationProperties(ThreadPoolConfig.class)
@Configuration
public class ThreadPoolExecutorConfig {
@Bean
public ThreadPoolExecutor executor(ThreadPoolConfig pool) {
return new ThreadPoolExecutor(
pool.getCoreSize(),
pool.getMaxSize(),
pool.getKeepAliveTime(),
TimeUnit.SECONDS,
new LinkedBlockingDeque<>(pool.getQueueSize()),
new ThreadFactoryBuilder().setNameFormat("cnSpider-%d").build(),
new ThreadPoolExecutor.CallerRunsPolicy()
);
}
}
int batchSize = list.size() <= threadPoolConfig.getCoreSize() ? list.size() : (list.size() / threadPoolConfig.getCoreSize());
for (int i = 0; i < list.size(); i += batchSize) {
List<Xxx> batch = list.subList(i, Math.min(i + batchSize, list.size()));
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
for (SpAdamsDatabase bo : batch) {
}
spAdamsDatabaseService.saveOrUpdateBatch(batch);
}, threadPoolExecutor).exceptionally(e -> {
return null;
});
}