This commit is contained in:
huahua
2024-07-24 11:54:20 +08:00
parent 64e4c2229b
commit 7cab85a021
4 changed files with 20 additions and 29 deletions

View File

@@ -18,37 +18,18 @@ public class ExecutorConfig {
private static final Logger logger = LoggerFactory.getLogger(ExecutorConfig.class);
private ExecutorService executorService;
private final AtomicBoolean isShutdown = new AtomicBoolean(false);
// @Bean
// public ExecutorService executorService() {
// ExecutorService executorService = Executors.newFixedThreadPool(12);
// return executorService;
// }
// @PreDestroy
// public void shutdown() {
// executorService.shutdown(); // 不立即停止所有任务,但不再接受新任务
// }
// @Bean
// public ExecutorService executorService() {
// // 考虑根据实际情况调整线程池大小
// int threadPoolSize = Runtime.getRuntime().availableProcessors() * 2;
// return Executors.newFixedThreadPool(threadPoolSize);
// }
@Bean
public ExecutorService executorService(@Value("${executor.threadPoolSize:24}") int threadPoolSize) {
public ExecutorService executorService(@Value("${lxk.executor.threadPoolSize:12}") int threadPoolSize) {
// 考虑根据配置文件或环境变量动态调整线程池大小
// 这里的 threadPoolSize 通过 @Value 注解可以从应用的配置文件中读取,默认值为 24
// 这里的 threadPoolSize 通过 @Value 注解可以从应用的配置文件中读取,默认值为 12
return Executors.newFixedThreadPool(threadPoolSize);
}
@PreDestroy
public void shutdown() {
if (!isShutdown.getAndSet(true)) {
// 尝试更彻底地关闭线程池
// 尝试更彻底地关闭线程池
if (executorService != null) {
executorService.shutdownNow(); // 阻止新任务提交并尝试停止当前正在执行的任务
try {
// 等待所有任务完成或等待一段时间