You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.3 KiB
Java
39 lines
1.3 KiB
Java
package com.glxp.api.config;
|
|
|
|
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.ThreadPoolExecutor;
|
|
|
|
/**
|
|
* 配置线程池
|
|
*/
|
|
@Configuration
|
|
@EnableAsync
|
|
public class TaskPoolConfig {
|
|
|
|
@Bean(name = "taskExecutor")
|
|
public ThreadPoolTaskExecutor taskExecutor() {
|
|
// 获取当前主机的cpu核心数
|
|
int threadCount = Runtime.getRuntime().availableProcessors();
|
|
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
|
|
//核心池的大小
|
|
taskExecutor.setCorePoolSize(threadCount);
|
|
//线程池最大线程数
|
|
taskExecutor.setMaxPoolSize(threadCount * 2);
|
|
//队列最大长度
|
|
taskExecutor.setQueueCapacity(200);
|
|
//线程空闲时间
|
|
taskExecutor.setKeepAliveSeconds(60);
|
|
//配置线程前缀
|
|
taskExecutor.setThreadNamePrefix("custom_executor");
|
|
//配置拒绝策略
|
|
taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
|
|
//执行初始化
|
|
taskExecutor.initialize();
|
|
return taskExecutor;
|
|
}
|
|
}
|