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

      

  • 相关阅读:
     随机选择数据库记录的方法
    交叉查询
    Delphi编辑器颜色设置
    Delphi Dll中多线程无法使用Synchronize同步的解决方法(转)
    Delphi FTP例子源码
    DELPHI之备忘四
    界面美化代码
    使Form响应滚轮事件
    配色卡
    Delphi http传输备忘
  • 原文地址:https://www.cnblogs.com/jiehanshi/p/13690988.html
Copyright © 2011-2022 走看看