66 lines
1.9 KiB
Plaintext
66 lines
1.9 KiB
Plaintext
package com.securityControl.task.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;
|
||
|
||
/**
|
||
* @author cw chen
|
||
* @description TODO
|
||
* @date 2022-08-22 14:42
|
||
*/
|
||
@EnableAsync
|
||
@Configuration
|
||
public class ThreadPoolConfig {
|
||
/**
|
||
* 核心线程数(默认线程数)
|
||
*/
|
||
private int corePoolSize = 10;
|
||
|
||
/**
|
||
* 最大线程数
|
||
*/
|
||
private int maxPoolSize = 10;
|
||
|
||
/**
|
||
* 允许线程空闲时间(单位:默认为秒)
|
||
*/
|
||
private int keepAliveTime = 10;
|
||
|
||
/**
|
||
* 缓冲队列数
|
||
*/
|
||
private int queueCapacity = 200;
|
||
|
||
/**
|
||
* 线程池名前缀
|
||
*/
|
||
private String threadNamePrefix = "custom-executor";
|
||
|
||
@Bean("TaskExecutor")
|
||
public ThreadPoolTaskExecutor taskExecutor() {
|
||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
||
//设置核心线程数
|
||
executor.setCorePoolSize(corePoolSize);
|
||
//设置最大线程数
|
||
executor.setMaxPoolSize(maxPoolSize);
|
||
//线程池所使用的缓冲队列
|
||
executor.setQueueCapacity(queueCapacity);
|
||
//等待任务在关机时完成--表明等待所有线程执行完
|
||
executor.setWaitForTasksToCompleteOnShutdown(true);
|
||
// 等待时间 (默认为0,此时立即停止),并没等待xx秒后强制停止
|
||
executor.setKeepAliveSeconds(keepAliveTime);
|
||
// 线程名称前缀
|
||
executor.setThreadNamePrefix(threadNamePrefix);
|
||
// 线程池对拒绝任务的处理策略
|
||
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
|
||
// 初始化
|
||
executor.initialize();
|
||
return executor;
|
||
}
|
||
|
||
}
|