动态的值
。多余
线程,继续保留corePoolSize数量的空闲线程,空闲线程需要等待活跃时间
(keepAliveSeconds)超过后再销毁。package com.xxxxx.config;
public class ThreadPoolNameKey {
/**
* 默认线程池
*/
public final static String customDefaultThreadPool = "customDefaultThreadPool";
/**
* 接收订单业务
*/
public final static String receiveOrderThreadPool = "receiveOrderThreadPool";
/**
* 处理订单业务
*/
public final static String handlerOrderThreadPool = "handlerOrderThreadPool";
}
package com.xxxxx.config;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
public class ThreadPoolConfig {
/**
* 核心线程池大小
*/
private int corePoolSize = 1;
/**
* 线程池中最大线程数
*/
private int maxPoolSize = Integer.MAX_VALUE;
/**
* 线程池中线程的空闲存活时间, 单位:秒
*/
private int keepAliveSeconds = 60;
/**
* 任务队列数量
*/
private int queueCapacity = Integer.MAX_VALUE;
/**
* 设置线程池中任务的等待时间, 单位:秒
*/
private int awaitTerminationSeconds = 180;
/**
* 线程池名的前缀
*/
private String threadNamePrefix = "ThreadPoolConfig";
/**
* 允许核心线程超时
*/
private boolean allowCoreThreadTimeOut = true;
public ThreadPoolConfig(int corePoolSize, int maxPoolSize, String threadNamePrefix){
this.corePoolSize = corePoolSize;
this.maxPoolSize = maxPoolSize;
this.threadNamePrefix = threadNamePrefix;
}
}
数值可以配置在yml文件中,首先在应用程序启动类,添加@EnableConfigurationProperties,
再在ThreadPoolConfig上面添加注解@ConfigurationProperties(), 指定yml配置项
package com.xxxxx.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
@Configuration
public class ThreadPool {
/**
* 默认线程池
* @return
*/
@Bean(name = ThreadPoolNameKey.customDefaultThreadPool)
public Executor customDefaultThreadPool(){
ThreadPoolConfig config = new ThreadPoolConfig(10, 20, ThreadPoolNameKey.customDefaultThreadPool);
return this.customThreadPool(config);
}
/**
* 接收订单业务-线程池
* @return
*/
@Bean(name = ThreadPoolNameKey.receiveOrderThreadPool)
public Executor receiveOrderThreadPool(){
ThreadPoolConfig config = new ThreadPoolConfig(20, 50, ThreadPoolNameKey.receiveOrderThreadPool);
return this.customThreadPool(config);
}
/**
* 处理订单业务-线程池
* @return
*/
@Bean(name = ThreadPoolNameKey.handlerOrderThreadPool)
public Executor handlerOrderThreadPool(){
ThreadPoolConfig config = new ThreadPoolConfig(10, 20, ThreadPoolNameKey.handlerOrderThreadPool);
return this.customThreadPool(config);
}
/**
* 构造线程池
* @return
*/
private Executor customThreadPool(ThreadPoolConfig config) {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//核心线程池大小
executor.setCorePoolSize(config.getCorePoolSize());
//线程池中最大线程数
executor.setMaxPoolSize(config.getMaxPoolSize());
//线程池中线程的空闲存活时间
executor.setKeepAliveSeconds(config.getKeepAliveSeconds());
//任务队列数量;线程数达到最大时,新任务会放在队列中排队等待执行。
executor.setQueueCapacity(config.getQueueCapacity());
//允许核心线程超时
executor.setAllowCoreThreadTimeOut(config.isAllowCoreThreadTimeOut());
//线程池名的前缀
executor.setThreadNamePrefix(config.getThreadNamePrefix());
//是否等待任务完成再结束线程池
executor.setWaitForTasksToCompleteOnShutdown(Boolean.TRUE);
//设置线程池中任务的等待时间,如果超过这个时间中断正在进行的任务并清除,确保任务都能被关闭,而不是阻塞住。
executor.setAwaitTerminationSeconds(config.getAwaitTerminationSeconds());
//任务调用线程间传递数据,或者为任务执行提供一些监控/统计信息
//executor.setTaskDecorator();
//线程工厂
//executor.setThreadFactory();
//Bean名称, 因为已经@Bean(name = "xxx"), 索引可不填
//executor.setBeanName();
//线程工厂的线程的优先级
//executor.setThreadPriority();
//是否应该创建守护线程
//executor.setDaemon();
//创建线程的线程组。
//executor.setThreadGroup();
//线程组名称
//executor.setThreadGroupName();
//拒绝策略
//AbortPolicy():默认值,直接抛出异常。
//CallerRunsPolicy():由调用的主线程执行该任务
//DiscardPolicy():直接丢弃任务
//DiscardOldestPolicy():丢弃线程队列最旧的未处理任务,添加新任务
//executor.setRejectedExecutionHandler();
executor.initialize();
return executor;
}
}
package com.xxxxx.component;
import com.pd.shopping.order.config.ThreadPoolNameKey;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class OrderComponent {
@Async(value = ThreadPoolNameKey.customDefaultThreadPool)
public void testDefaultThreadPool(Integer i){
try {
System.out.print(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss") + " >> 默认线程名称-开始:" + "索引-" + i + " >> " + Thread.currentThread().getName() + "\n");
Thread.sleep(1000L);
System.out.print(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss") + " >> 默认线程名称-结束:" + "索引-" + i + " >> " + Thread.currentThread().getName() + "\n");
} catch (Exception e){
System.out.print("睡眠线程异常:" + e.toString());
}
}
@Async(value = ThreadPoolNameKey.receiveOrderThreadPool)
public void testReceiveOrderThreadPool(Integer i){
try {
System.out.print(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss") + " >> 接单线程名称-开始:" + "索引-" + i + " >> " + Thread.currentThread().getName() + "\n");
Thread.sleep(1000L);
System.out.print(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss") + " >> 接单线程名称-结束:" + "索引-" + i + " >> " + Thread.currentThread().getName() + "\n");
} catch (Exception e){
System.out.print("睡眠线程异常:" + e.toString());
}
}
@Async(value = ThreadPoolNameKey.handlerOrderThreadPool)
public void testHandlerOrderThreadPool(Integer i){
try {
System.out.print(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss") + " >> 处理订单线程名称-开始:" + "索引-" + i + " >> " + Thread.currentThread().getName() + "\n");
Thread.sleep(1000L);
System.out.print(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss") + " >> 处理订单线程名称-结束:" + "索引-" + i + " >> " + Thread.currentThread().getName() + "\n");
} catch (Exception e){
System.out.print("睡眠线程异常:" + e.toString());
}
}
}
获取线程池对象, 可在运行时动态修改线程池部分参数
/**
* 注入线程池,来获取线程池对象
*
* 自定义线程池名称 customDefaultThreadPool、receiveOrderThreadPool、handlerOrderThreadPool
*
* 通过变量名称,可以自动匹配 @Bean("customDefaultThreadPool") 这个线程池
*/
@Lazy
@Autowired
private ThreadPoolTaskExecutor customDefaultThreadPool;
@Lazy延时注入,主要防止应用程序启动时,@Configuration还没完成,导致获取线程池对象失败。
customDefaultThreadPool对象中中可以重新set部分参数。
@Lazy
@Autowired
private ThreadPoolTaskExecutor customDefaultThreadPool;
public String testThreadPool(int forEachNum) {
int activeCount = customDefaultThreadPool.getActiveCount();
int corePoolSize = customDefaultThreadPool.getPoolSize();
System.out.print("customDefault activeCount:" + activeCount + "\n");
System.out.print("customDefault corePoolSize:" + corePoolSize + "\n");
for (Integer i = 1 ; i <= forEachNum; i ++){
try {
orderComponent.testDefaultThreadPool(i);
//orderComponent.testReceiveOrderThreadPool(i);
//orderComponent.testHandlerOrderThreadPool(i);
} catch (RejectedExecutionException e){
return "线程资源不够用, 拒绝执行任务,请稍后重试";
} catch (Exception e){
return "";
}
}
return "success";
}