ThreadPoolTaskExecutor线程池参数配置

ThreadPoolTaskExecutor线程池参数配置一、线程池配置1、ThreadPoolConfigimportorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.scheduling.annotation.EnableAsync;importorg.springframework.scheduling.concurrent.Threa

大家好,又见面了,我是你们的朋友全栈君。

一、线程池配置

1、ThreadPoolConfig

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class ThreadPoolConfig { 
   

    private final static int AVAILABLE_PROCESSOR = Runtime.getRuntime().availableProcessors();

    /** * 通用线程池配置 */
    @Bean("taskPoolExecutor")
    public Executor taskExecutor() { 
   
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        //设置线程池参数信息
        taskExecutor.setCorePoolSize(6);
        taskExecutor.setMaxPoolSize(6*2);
        taskExecutor.setQueueCapacity(12);
        taskExecutor.setKeepAliveSeconds(60);
        taskExecutor.setThreadNamePrefix("taskPoolExecutor--");
        taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        taskExecutor.setAwaitTerminationSeconds(60);
        //修改拒绝策略为使用当前线程执行
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        //初始化线程池
        taskExecutor.initialize();
        return taskExecutor;
    }

     /** * 发送短信线程池 * @return */
    @Bean("sendSmsThreadPool")
    public Executor sendSmsThreadPool() { 
   
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        //设置线程池参数信息
        taskExecutor.setCorePoolSize(AVAILABLE_PROCESSOR);
        taskExecutor.setMaxPoolSize(AVAILABLE_PROCESSOR + 1);
        taskExecutor.setQueueCapacity(256);
        taskExecutor.setKeepAliveSeconds(20);
        taskExecutor.setThreadNamePrefix("sendSmsThreadPool--");
        taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        taskExecutor.setAwaitTerminationSeconds(60);
        //修改拒绝策略为使用当前线程执行
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //初始化线程池
        taskExecutor.initialize();
        return taskExecutor;
    }

    /** * 发送邮件线程池 * @return */
    @Bean("sendEmailThreadPool")
    public Executor sendEmailThreadPool() { 
   
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        //设置线程池参数信息
        taskExecutor.setCorePoolSize(AVAILABLE_PROCESSOR);
        taskExecutor.setMaxPoolSize(AVAILABLE_PROCESSOR + 1);
        taskExecutor.setQueueCapacity(256);
        taskExecutor.setKeepAliveSeconds(20);
        taskExecutor.setThreadNamePrefix("sendEmailThreadPool--");
        taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        taskExecutor.setAwaitTerminationSeconds(60);
        //修改拒绝策略为使用当前线程执行
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //初始化线程池
        taskExecutor.initialize();
        return taskExecutor;
    }

    @Bean(name = "threadPoolTaskExecutor")
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() { 
   
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(5);
        executor.setQueueCapacity(10);
        executor.setKeepAliveSeconds(300);
        executor.setThreadNamePrefix("thread-ayokredit"); //线程名称
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return executor;
    }

    
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/136737.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • PropertyDescriptor 详解

    PropertyDescriptor 详解PropertyDescriptor详解](http://blog.csdn.net/z69183787/article/details/8443777)转自http://blog.csdn.net/z69183787/article/details/8443777packagecom.zhoushun;importjava.lang.reflect.Method;importjava

    2022年10月1日
    0
  • 如何关闭ESLint,一次成功

    如何关闭ESLint,一次成功ESLint可以用来识别ECMAScript,并且按照规则给出报告的代码检测工具,使用它可以避免低级错误和统一代码的风格。但是有时候新手会被ESLint的报错阻止程序的运行,这时候我们就想关闭这个ESLint了。vue项目中关闭ESLint方法:找到build文件夹—>webpack.base.conf.js—->module然后重启服务,npmrundev就可以…

    2022年5月5日
    243
  • python计算坐标点欧式距离_计算Python Numpy向量之间的欧氏距离实例

    python计算坐标点欧式距离_计算Python Numpy向量之间的欧氏距离实例计算PythonNumpy向量之间的欧氏距离,已知vec1和vec2是两个Numpy向量,欧氏距离计算如下:importnumpydist=numpy.sqrt(numpy.sum(numpy.square(vec1-vec2)))或者直接:dist=numpy.linalg.norm(vec1-vec2)#补充知识:Python中计算两个数据点之间的欧式距离,一个点到数据集中其他点的距离之和#如下所示:#计算数两个数据点之间的欧式距离importn

    2022年6月19日
    49
  • easyui window refresh 刷新两次解决办法

    easyui window refresh 刷新两次解决办法这样写刷新两次$("#changeMwsAccountWin").window(‘refresh’,"adsfasdf.php"’);$("#changeMwsAccountWin").window(‘open’); 这样写刷新一次$("#changeMwsAccountWin").window(‘open’);$("#changeMwsAccountWin").wi…

    2022年7月18日
    13
  • 俄罗斯方块(C语言实现)

    俄罗斯方块(C语言实现)文章目录游戏说明游戏效果展示游戏代码游戏代码详解游戏框架构建隐藏光标光标跳转初始化界面初始化方块信息颜色设置画出方块空格覆盖合法性判断游戏主体逻辑函数判断得分与结束主函数游戏说明俄罗斯方块相信大家都知道,这里就不再介绍什么游戏背景了,我这里对本代码实现的俄罗斯方块作一些说明:按方向键的左右键可实现方块的左右移动。按方向键的下键可实现方块的加速下落。按空格键可实现方块的顺时针旋转。按Esc键可退出游戏。按S键可暂停游戏,暂停游戏后按任意键继续游戏。按R键可重新开始游戏。除此之外,本游戏还

    2022年5月19日
    39
  • dota5显示正在连接协调服务器,win10系统打开dota2提示已连接至DOTA2游戏协调服务器正在登陆中如何解决…

    dota5显示正在连接协调服务器,win10系统打开dota2提示已连接至DOTA2游戏协调服务器正在登陆中如何解决…近日有win10系统用户要在电脑中玩dota2游戏的时候,发现一打开dota2提示已连接至DOTA2游戏协调服务器正在登陆中,该怎么办呢,本文就给大家讲解一下win10系统打开dota2提示已连接至DOTA2游戏协调服务器正在登陆中的详细解决步骤。解决方法一、1、开始按钮在搜索中输入CMD,打开第一个在弹出框输入:netshwinsockresetcatalog;2、直接复制然后再弹出框中右…

    2022年5月17日
    93

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号