spring创建线程池和springboot创建线程池
spring
<!-- spring线程池 -->
<bean id = "task" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<!-- 线程池维护线程的最少数量 -->
<property name="corePoolSize" value="2"></property>
<!-- 允许空闲时间 -->
<property name="keepAliveSeconds" value="200"></property>
<!-- 线程池维护线程的最大数量 -->
<property name="maxPoolSize" value="5"></property>//配置为5,实际最大线程为maxPoolSize+1=6
<!-- 缓存队列 -->
<property name="queueCapacity" value="1"></property>
<!-- 对拒绝task的处理策略 -->
<property name="rejectedExecutionHandler">
<bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />
</property>
</bean>
原文链接:https://blog.csdn.net/weixin\_42861564/article/details/81587894
spring boot
@Configuration
public class AsyncConfiguration {
@Bean("async-executor")
public ThreadPoolExecutor asyncExecutor() {
int cpu = Runtime.getRuntime().availableProcessors();
return new ThreadPoolExecutor(cpu, cpu << 2, 0L, TimeUnit.MILLISECONDS, new LinkedTransferQueue<>());
}
@Bean("async-redis")
public ThreadPoolExecutor redisExecutor() {
return new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedTransferQueue<>());
}
}
还没有评论,来说两句吧...