parent
983b32ccf9
commit
32512af319
@ -0,0 +1,49 @@ |
||||
package com.dxhy.order.consumer.config; |
||||
|
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; |
||||
|
||||
import java.util.concurrent.Executor; |
||||
import java.util.concurrent.ThreadPoolExecutor; |
||||
|
||||
/** |
||||
* Created by IntelliJ IDEA. |
||||
* |
||||
* @author: zhenghaiyang |
||||
* Date: 2023-02-06 |
||||
* Description: |
||||
*/ |
||||
@Configuration |
||||
public class ThreadPoolConfig { |
||||
|
||||
//参数初始化
|
||||
private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors(); |
||||
//核心线程数量大小
|
||||
private static final int corePoolSize = Math.max(2, Math.min(CPU_COUNT - 1, 4)); |
||||
//线程池最大容纳线程数
|
||||
private static final int maxPoolSize = CPU_COUNT * 2 + 1; |
||||
//阻塞队列
|
||||
private static final int workQueue = 20; |
||||
//线程空闲后的存活时长
|
||||
private static final int keepAliveTime = 30; |
||||
|
||||
@Bean("asyncExecutor") |
||||
public Executor getAsyncExecutor() { |
||||
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor(); |
||||
//核心线程数
|
||||
threadPoolTaskExecutor.setCorePoolSize(corePoolSize); |
||||
//最大线程数
|
||||
threadPoolTaskExecutor.setMaxPoolSize(maxPoolSize); |
||||
//等待队列
|
||||
threadPoolTaskExecutor.setQueueCapacity(workQueue); |
||||
//线程前缀
|
||||
threadPoolTaskExecutor.setThreadNamePrefix("async-"); |
||||
//线程池维护线程所允许的空闲时间,单位为秒
|
||||
threadPoolTaskExecutor.setKeepAliveSeconds(keepAliveTime); |
||||
// 线程池对拒绝任务(无线程可用)的处理策略
|
||||
threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); |
||||
threadPoolTaskExecutor.initialize(); |
||||
return threadPoolTaskExecutor; |
||||
} |
||||
} |
Loading…
Reference in new issue