From 4465cc57e7b6225a2a5ae9d728a15f0509600195 Mon Sep 17 00:00:00 2001 From: anthonywj Date: Mon, 27 Feb 2023 19:41:45 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E6=B1=A0=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/glxp/api/task/TaskExecutorConfig.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/main/java/com/glxp/api/task/TaskExecutorConfig.java diff --git a/src/main/java/com/glxp/api/task/TaskExecutorConfig.java b/src/main/java/com/glxp/api/task/TaskExecutorConfig.java new file mode 100644 index 000000000..22ce458a9 --- /dev/null +++ b/src/main/java/com/glxp/api/task/TaskExecutorConfig.java @@ -0,0 +1,34 @@ +package com.glxp.api.task; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.AsyncConfigurer; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +import java.lang.reflect.Method; +import java.util.concurrent.Executor; + +@EnableAsync +@Configuration +public class TaskExecutorConfig implements AsyncConfigurer { + + private static final Logger log = LoggerFactory.getLogger(TaskExecutorConfig.class); + + + @Override + public Executor getAsyncExecutor() { + ThreadPoolTaskExecutor threadPool = new ThreadPoolTaskExecutor(); + threadPool.setCorePoolSize(10);//当前线程数 + threadPool.setMaxPoolSize(20);// 最大线程数 + threadPool.setQueueCapacity(5);//线程池所使用的缓冲队列 + threadPool.setWaitForTasksToCompleteOnShutdown(true);//等待任务在关机时完成--表明等待所有线程执行完 + threadPool.setAwaitTerminationSeconds(60 * 15);// 等待时间 (默认为0,此时立即停止),并没等待xx秒后强制停止 + threadPool.setThreadNamePrefix("MyAsync-");// 线程名称前缀 + threadPool.initialize(); // 初始化 + return threadPool; + } + +}