package com.ruoyi.common.config.thread;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
/**
* 线程池配置
**/
@Configuration
public class ThreadPoolConfig {
// 核心线程池大小
private int corePoolSize = 50;
// 最大可创建的线程数
private int maxPoolSize = 200;
// 队列最大长度
private int queueCapacity = 1000;
// 线程池维护线程所允许的空闲时间
private int keepAliveSeconds = 300;
@Bean(name = "threadPoolTaskExecutor")
public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setMaxPoolSize(maxPoolSize);
executor.setCorePoolSize(corePoolSize);
executor.setQueueCapacity(queueCapacity);
executor.setKeepAliveSeconds(keepAliveSeconds);
executor.setThreadNamePrefix("ItmcAsync-");
// 线程池对拒绝任务(无线程可用)的处理策略
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return executor;
}
@Bean(name = "threadPoolResourceExchange")
public ThreadPoolTaskExecutor threadPoolResourceExchange() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setMaxPoolSize(20);
executor.setCorePoolSize(4);
executor.setQueueCapacity(200);
executor.setKeepAliveSeconds(keepAliveSeconds);
executor.setThreadNamePrefix("ResourceAsync-");
// 线程池对拒绝任务(无线程可用)的处理策略
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return executor;
}
//使用方法:在serviceImpl实现类的方法上加注解@Async("threadPoolTaskExecutor"),举例如下
/*
@Override
@Async("threadPoolTaskExecutor")
public void tagRemarkAiGen(List<SysTag> tagList, String onlineBaseId) {
for (SysTag sysTag : tagList) {
if (ICommons.isNullOrEmpty(sysTag.getRemark())) {
String remark = AIUtil.tagRemarkAIAgent(sysTag.getTagName(), onlineBaseId);
if (!ICommons.isNullOrEmpty(remark)) {
sysTag.setRemark(remark);
sysTagMapper.updateSysTag(sysTag);
}
}
}
}
*/
@Bean(name = "taskExecutor")
public Executor taskExecutor() {
ThreadPoolTaskExecutor poolTaskExecutor = new ThreadPoolTaskExecutor();
// 获取当前虚拟机可用的最大值
int core = Runtime.getRuntime().availableProcessors();
// 设置核心线程数
poolTaskExecutor.setCorePoolSize(core - 1);
// 设置最大线程数
poolTaskExecutor.setMaxPoolSize(2 * core + 1);
// 允许线程的空闲时间,超过了核心线程数之外的线程,在空闲时间到达后会被销毁
poolTaskExecutor.setKeepAliveSeconds(60);
// 传入值大于1,底层队列使用的是LinkedBlockingQueue,默认为SynchronousQueue
poolTaskExecutor.setQueueCapacity(40);
// 设置线程名称前缀
poolTaskExecutor.setThreadNamePrefix("Thread-task-time");
// 设置拒绝策略
// rejection-policy:当pool已经达到max size的时候,如何处理新任务
// CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
poolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 设置线程池关闭的时候等待所有任务执行完毕在继续销毁
poolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
// 线程池中的任务的等待时间,如果超出时间没有销毁就强制销毁,以确保应用最后能够被关闭,而不是阻塞住
poolTaskExecutor.setAwaitTerminationSeconds(60);
poolTaskExecutor.initialize();
return poolTaskExecutor;
}
}