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;
        }
    }
    

      

  • 相关阅读:
    jackson、gson、fastjson
    jackson、gson、fastjson
    Java 常见面试题
    java8的新特性2
    关于mybatis的注释问题
    java PropertyDescriptor 应用及源码分析
    Java 反射总结
    java8的新特性1
    vo、po、dto、bo、pojo、entity、mode如何区分
    倒计时 不加锁 老代码
  • 原文地址:https://www.cnblogs.com/jiehanshi/p/13690988.html
Copyright © 2011-2022 走看看