1.设置线程池常量
/**
* 线程池常量
*/
public class ThreadPoolConstants {
/**
* 用户线程前缀
*/
public final static String USER_THREAD_PREFIX = "user-thread";
/**
* 学校线程前缀
*/
public final static String SCHOOL_THREAD_PREFIX = "school-thread";
/**
* 线程池bean后缀名
*/
public final static String THREA_BEAN_SUFFIX = "-exector-bean";
/**
* 运行线程名称后缀
*/
public final static String RUNNING_THREAD_SUFFIX = "-pool-task-";
/**
* 线程参数配置-核线程数
*/
public final static String THREAD_POOL_CORE_SIZE = "corePoolSize";
/**
* 线程参数配置-最大线程数
*/
public final static String THREAD_POOL_MAX_SIZE = "maxPoolSize";
/**
* 线程参数配置-线程存活时长
*/
public final static String THREAD_POOL_KEEP_ALIVE = "keepAliveSeconds";
/**
* 线程参数配置-队列长度
*/
public final static String THREAD_POOL_QUEUE_CAPACITY = "queueCapacity";
}
2.配置核心
/**
* 线程池抽象类
*/
@Data
public abstract class AbstractExecutorPool {
private int corePoolSize;
private int maxPoolSize;
private int keepAliveSeconds;
private int queueCapacity;
}
该列配置配置到yml文件或者properties文件内 下面是配置到properties文件内
thread-pool.user-thread.corePoolSize=1
thread-pool.user-thread.maxPoolSize=1
thread-pool.user-thread.keepAliveSeconds=120
thread-pool.user-thread.queueCapacity=1
thread-pool.school-thread.corePoolSize=2
thread-pool.school-thread.maxPoolSize=2
thread-pool.school-thread.keepAliveSeconds=60
thread-pool.school-thread.queueCapacity=2
3.配置多个线程池
/**
* 用户线程池参数类
*/
@Component
@ConfigurationProperties(prefix = "thread-pool.user-thread")
@Data
public class UserThreadPool extends AbstractExecutorPool {
/**
* 线程池前缀名称:user-thread-pool-task-
*/
private final String threadNamePrefix = ThreadPoolConstants.USER_THREAD_PREFIX + ThreadPoolConstants.RUNNING_THREAD_SUFFIX;
}
/**
* 学校线程池参数类
*/
@Component
@ConfigurationProperties(prefix = "thread-pool.school-thread")
@Data
public class SchoolThreadPool extends AbstractExecutorPool {
/**
* 线程池前缀名称:school-thread-pool-task-
*/
private final String threadNamePrefix = ThreadPoolConstants.SCHOOL_THREAD_PREFIX + ThreadPoolConstants.RUNNING_THREAD_SUFFIX;
}
4.将线程池交由spring
/**
* 线程池配置类
*/
@Configuration
@EnableAsync
@Slf4j
public class ThreadPoolConfig {
@Autowired
private UserThreadPool userThreadPool;
@Autowired
private SchoolThreadPool schoolThreadPool;
/**
* 创建用户线程池
* beanName: "user-thread-exector-bean"
*/
@Bean(name = ThreadPoolConstants.USER_THREAD_PREFIX + ThreadPoolConstants.THREA_BEAN_SUFFIX)
public ThreadPoolTaskExecutor userThreadExector() {
return initExcutor(userThreadPool, userThreadPool.getThreadNamePrefix(), (r, executor) -> {
log.info("userThreadExector队列已满,根据业务自行处理。。。");
});
}
/**
* 创建学校线程池
* beanName: "school-thread-exector-bean"
*/
@Bean(name = ThreadPoolConstants.SCHOOL_THREAD_PREFIX + ThreadPoolConstants.THREA_BEAN_SUFFIX)
public ThreadPoolTaskExecutor schoolThreadExector() {
return initExcutor(schoolThreadPool, schoolThreadPool.getThreadNamePrefix(),(r, executor) -> {
log.info("schoolThreadExector队列已满,根据业务自行处理。。。");
});
}
/**
* 初始化线程池
* @param abstractExecutorPool
* @param threadName
* @param rejectedExecutionHandler
* @return
*/
private ThreadPoolTaskExecutor initExcutor(AbstractExecutorPool abstractExecutorPool, String threadName, RejectedExecutionHandler rejectedExecutionHandler){
// 创建线程池并设置参数
ThreadPoolTaskExecutor threadPool = new ThreadPoolTaskExecutor();
threadPool.setCorePoolSize(abstractExecutorPool.getCorePoolSize());
threadPool.setMaxPoolSize(abstractExecutorPool.getMaxPoolSize());
threadPool.setKeepAliveSeconds(abstractExecutorPool.getKeepAliveSeconds());
threadPool.setQueueCapacity(abstractExecutorPool.getQueueCapacity());
threadPool.setThreadNamePrefix(threadName);
threadPool.setRejectedExecutionHandler(rejectedExecutionHandler);
return threadPool;
}
}
5.使用线程池(springboot启动类加上@EnableAsync)
/**
* 测试线程池service
*/
@Service
public class TestThreadPoolService {
@Autowired
@Qualifier(ThreadPoolConstants.USER_THREAD_PREFIX + ThreadPoolConstants.THREA_BEAN_SUFFIX)
private ThreadPoolTaskExecutor userTheadExector;
@Autowired
@Qualifier(ThreadPoolConstants.SCHOOL_THREAD_PREFIX + ThreadPoolConstants.THREA_BEAN_SUFFIX)
private ThreadPoolTaskExecutor schoolThreadExector;
/**
* 测试线程
*/
public void testUserThread(){
userTheadExector.execute(() ->{
try {
// 设置睡眠时间让线程阻塞便于观察
Thread.sleep(5000);
System.out.println(Thread.currentThread().getName() + "执行testUserThread业务完毕.....................");
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
/**
* 测试线程
*/
public void testSchoolThread(){
schoolThreadExector.execute(() ->{
try {
// 设置睡眠时间让线程阻塞便于观察
Thread.sleep(3000);
System.out.println(Thread.currentThread().getName() + "执行testSchoolThread业务完毕.......................");
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
public String getThreadPoolInfo()
{
return "user-thread-pool:"+ "corePoolSize:" + userTheadExector.getCorePoolSize() + "maxPoolSize:"+ userTheadExector.getMaxPoolSize() + "keepAliveSeconds:"+userTheadExector.getKeepAliveSeconds()+"\n" +
"school-thread-pool:"+ "corePoolSize:" + schoolThreadExector.getCorePoolSize() + "maxPoolSize:"+ schoolThreadExector.getMaxPoolSize() + "keepAliveSeconds:"+schoolThreadExector.getKeepAliveSeconds();
}
}
6.测试
@RestController
@RequestMapping("/thread")
public class TestThreadPoolController {
Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
TestThreadPoolService testThreadPoolService;
@RequestMapping("/test")
public String get(){
testThreadPoolService.testUserThread();
testThreadPoolService.testSchoolThread();
return "success";
}
}