init
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package com.newyetai.usercenter.common.threadPools;
|
||||
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
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 on 2019-11-05
|
||||
*/
|
||||
@Configuration
|
||||
@EnableAsync
|
||||
public class AsyncExecutorConfig {
|
||||
|
||||
@Value("${async-executor.core_pool_size:10}")
|
||||
private int corePoolSize;
|
||||
@Value("${async-executor.max_pool_size:50}")
|
||||
private int maxPoolSize;
|
||||
@Value("${async-executor.queue-capacity:1000}")
|
||||
private int queueCapacity;
|
||||
@Value("${async-executor.name-prefix:async-}")
|
||||
private String namePrefix;
|
||||
|
||||
|
||||
@Bean("asyncExecutor")
|
||||
public Executor asyncExecutor() {
|
||||
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
|
||||
taskExecutor.setCorePoolSize(corePoolSize);// 线程池核心线程数
|
||||
taskExecutor.setMaxPoolSize(maxPoolSize);// 线程池最大线程数
|
||||
taskExecutor.setQueueCapacity(queueCapacity);// 缓冲队列
|
||||
taskExecutor.setThreadNamePrefix(namePrefix);// 线程名称前缀
|
||||
taskExecutor.setKeepAliveSeconds(60);// 线程空闲后的最大存活时间
|
||||
// rejection-policy:当pool已经达到max size的时候,如何处理新任务
|
||||
// CALLER_RUNS:不在新线程中执行任务,而是由调用者所在的线程来执行
|
||||
// 对拒绝task的处理策略
|
||||
taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
|
||||
// 执行初始化
|
||||
taskExecutor.initialize();
|
||||
return taskExecutor;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user