|
@@ -1,43 +1,52 @@
|
|
|
-//package com.zd.laboratory.config;
|
|
|
|
|
-//
|
|
|
|
|
-//import org.slf4j.Logger;
|
|
|
|
|
-//import org.slf4j.LoggerFactory;
|
|
|
|
|
-//import org.springframework.context.annotation.Bean;
|
|
|
|
|
-//import org.springframework.context.annotation.Configuration;
|
|
|
|
|
-//import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
|
|
-//
|
|
|
|
|
-//import java.util.concurrent.Executor;
|
|
|
|
|
-//import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
|
-//
|
|
|
|
|
-///**
|
|
|
|
|
-// * @Author: zhoupan
|
|
|
|
|
-// * @Date: 2021/11/17/14:39
|
|
|
|
|
-// * @Description:
|
|
|
|
|
-// */
|
|
|
|
|
-//@Configuration
|
|
|
|
|
-//public class ExecutorConfig {
|
|
|
|
|
-//
|
|
|
|
|
-//
|
|
|
|
|
-// private static final Logger logger = LoggerFactory.getLogger(ExecutorConfig.class);
|
|
|
|
|
-//
|
|
|
|
|
-// @Bean(name = "labExecutor")
|
|
|
|
|
-// public Executor asyncServiceExecutor() {
|
|
|
|
|
-// logger.info("start asyncServiceExecutor");
|
|
|
|
|
-// ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
|
|
|
|
-// //配置核心线程数
|
|
|
|
|
-// executor.setCorePoolSize(5);
|
|
|
|
|
-// //配置最大线程数
|
|
|
|
|
-// executor.setMaxPoolSize(8);
|
|
|
|
|
-// //配置队列大小
|
|
|
|
|
-// executor.setQueueCapacity(99999);
|
|
|
|
|
-// //配置线程池中的线程的名称前缀
|
|
|
|
|
-// executor.setThreadNamePrefix("async-service-");
|
|
|
|
|
-//
|
|
|
|
|
-// // rejection-policy:当pool已经达到max size的时候,如何处理新任务
|
|
|
|
|
-// // CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
|
|
|
|
|
-// executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
|
|
|
|
|
-// //执行初始化
|
|
|
|
|
-// executor.initialize();
|
|
|
|
|
-// return executor;
|
|
|
|
|
-// }
|
|
|
|
|
-//}
|
|
|
|
|
|
|
+package com.zd.laboratory.config;
|
|
|
|
|
+
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
|
|
+import org.springframework.scheduling.annotation.EnableAsync;
|
|
|
|
|
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.concurrent.Executor;
|
|
|
|
|
+import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @Author: zhoupan
|
|
|
|
|
+ * @Date: 2021/11/17/14:39
|
|
|
|
|
+ * @Description:
|
|
|
|
|
+ */
|
|
|
|
|
+@EnableAsync
|
|
|
|
|
+@Configuration
|
|
|
|
|
+public class ExecutorConfig {
|
|
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(ExecutorConfig.class);
|
|
|
|
|
+
|
|
|
|
|
+ @Bean(name = "labExecutor")
|
|
|
|
|
+ public Executor asyncServiceExecutor() {
|
|
|
|
|
+ logger.info("start asyncServiceExecutor");
|
|
|
|
|
+ ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
|
|
|
|
+ //配置核心线程数
|
|
|
|
|
+ executor.setCorePoolSize(10);
|
|
|
|
|
+ //线程池维护线程的最大数量,只有在缓冲队列满了之后才会申请超过核心线程数的线程
|
|
|
|
|
+ executor.setMaxPoolSize(50);
|
|
|
|
|
+ //缓存队列
|
|
|
|
|
+ executor.setQueueCapacity(50);
|
|
|
|
|
+ //许的空闲时间,当超过了核心线程出之外的线程在空闲时间到达之后会被销毁
|
|
|
|
|
+ executor.setKeepAliveSeconds(200);
|
|
|
|
|
+ //配置线程池中的线程的名称前缀
|
|
|
|
|
+ executor.setThreadNamePrefix("async-service-");
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 当线程池的任务缓存队列已满并且线程池中的线程数目达到maximumPoolSize,如果还有任务到来就会采取任务拒绝策略
|
|
|
|
|
+ * 通常有以下四种策略:
|
|
|
|
|
+ * ThreadPoolExecutor.AbortPolicy:丢弃任务并抛出RejectedExecutionException异常。
|
|
|
|
|
+ * ThreadPoolExecutor.DiscardPolicy:也是丢弃任务,但是不抛出异常。
|
|
|
|
|
+ * ThreadPoolExecutor.DiscardOldestPolicy:丢弃队列最前面的任务,然后重新尝试执行任务(重复此过程)
|
|
|
|
|
+ * ThreadPoolExecutor.CallerRunsPolicy:重试添加当前的任务,自动重复调用 execute() 方法,直到成功
|
|
|
|
|
+ */
|
|
|
|
|
+ // rejection-policy:当pool已经达到max size的时候,如何处理新任务
|
|
|
|
|
+ // CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
|
|
|
|
|
+ executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
|
|
|
|
|
+ //执行初始化
|
|
|
|
|
+ executor.initialize();
|
|
|
|
|
+ return executor;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|