zoukankan      html  css  js  c++  java
  • springboot 使用线程池

    @Configuration
    @EnableAsync
    public class ExecutePoolConfig {
    
        /**
         * 核心线程数
         */
        @Value("${taskThreadPool.corePoolSize}")
        private int corePoolSize;
    
        /**
         * 最大线程数
         */
        @Value("${taskThreadPool.maxPoolSize}")
        private int maxPoolSize;
    
        /**
         * 线程活跃时间
         */
        @Value("${taskThreadPool.keepAliveSeconds}")
        private int keepAliveSeconds;
    
        /**
         * 队列容量
         */
        @Value("${taskThreadPool.queueCapacity}")
        private int queueCapacity;
    
        /**
         * 线程池,如果有特殊的业务场景,可以自行再添加线程池
         * 使用示例   在需要使用线程池的方法上增加注解 @Async("taskExecutor")
         * @return
         */
        @Bean("taskExecutor")
        public Executor taskExecutor() {
            ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
            //核心线程池大小
            executor.setCorePoolSize(corePoolSize);
            //最大线程数
            executor.setMaxPoolSize(maxPoolSize);
            //队列容量
            executor.setQueueCapacity(queueCapacity);
            //活跃时间
            executor.setKeepAliveSeconds(keepAliveSeconds);
            //线程名字前缀
            executor.setThreadNamePrefix("Service-");
    
            // setRejectedExecutionHandler:当pool已经达到max size的时候,如何处理新任务
            // CallerRunsPolicy:不在新线程中执行任务,而是由调用者所在的线程来执行,即变为单线程处理
            executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
            executor.initialize();
            return executor;
        }
    }
    

      

  • 相关阅读:
    (二)WCF的Binding模型
    (一)WCF基础
    EF映射——从数据库更新实体
    没有什么不可能(1)
    MySQL SQL Training
    MySQL 并发事务问题以及事务的隔离级别
    MySQL 数据库面试题
    MySQL create table语法详解
    MySQL create table语法中的key与index的区别
    MySQL 官方样板数据库sakila
  • 原文地址:https://www.cnblogs.com/jiehanshi/p/13690988.html
Copyright © 2011-2022 走看看