spring创建线程池和springboot创建线程池

小鱼儿 2023-02-09 14:28 312阅读 0赞

spring

  1. <!-- spring线程池 -->
  2. <bean id = "task" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
  3. <!-- 线程池维护线程的最少数量 -->
  4. <property name="corePoolSize" value="2"></property>
  5. <!-- 允许空闲时间 -->
  6. <property name="keepAliveSeconds" value="200"></property>
  7. <!-- 线程池维护线程的最大数量 -->
  8. <property name="maxPoolSize" value="5"></property>//配置为5,实际最大线程为maxPoolSize+1=6
  9. <!-- 缓存队列 -->
  10. <property name="queueCapacity" value="1"></property>
  11. <!-- 对拒绝task的处理策略 -->
  12. <property name="rejectedExecutionHandler">
  13. <bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />
  14. </property>
  15. </bean>

原文链接:https://blog.csdn.net/weixin\_42861564/article/details/81587894

spring boot

  1. @Configuration
  2. public class AsyncConfiguration {
  3. @Bean("async-executor")
  4. public ThreadPoolExecutor asyncExecutor() {
  5. int cpu = Runtime.getRuntime().availableProcessors();
  6. return new ThreadPoolExecutor(cpu, cpu << 2, 0L, TimeUnit.MILLISECONDS, new LinkedTransferQueue<>());
  7. }
  8. @Bean("async-redis")
  9. public ThreadPoolExecutor redisExecutor() {
  10. return new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedTransferQueue<>());
  11. }
  12. }

发表评论

表情:
评论列表 (有 0 条评论,312人围观)

还没有评论,来说两句吧...

相关阅读

    相关 java 创建线

    线程池,其实就是一个容纳多个线程的容器,其中的线程可以反复使用,省去了频繁创建线程对象的操作, 无需反复创建线程而消耗过多资源。 我们详细的解释一下为什么要使用线程池?

    相关 线 线创建

    一简介 线程的使用在java中占有极其重要的地位,在jdk1.4极其之前的jdk版本中,关于线程池的使用是极其简陋的。在jdk1.5之后这一情况有了很大的改观。Jdk1.