1.配置类

package cn.com.bonc.util;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; @Configuration
@EnableAsync
public class MyThread { private int corePoolSize = 10;//线程池维护线程的最少数量 private int maxPoolSize = 30;//线程池维护线程的最大数量 private int queueCapacity = 8; //缓存队列 private int keepAlive = 60;//允许的空闲时间 @Bean
public Executor myExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(queueCapacity);
executor.setThreadNamePrefix("mqExecutor-");
     // rejection-policy:当pool已经达到max size的时候,如何处理新任务  
        // CALLER_RUNS:不在新线程中执行任务,而是由调用者所在的线程来执行 
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); //对拒绝task的处理策略
executor.setKeepAliveSeconds(keepAlive);
executor.initialize();
return executor;
} }

2.注解使用

@Async("myExecutor") //配置类中的方法名

3.启动类添加

@EnableAsync

spring boot 线程池配置的更多相关文章

  1. Spring Boot 线程池

    参考 SpringBoot 线程池 程序猿DD-Spring Boot使用@Async实现异步调用:自定义线程池 如何优雅的使用和理解线程池 Spring Boot线程池的使用心得 博客园-Sprin ...

  2. Spring Boot 线程池的使用和扩展 - 转载

    转载:http://blog.csdn.net/boling_cavalry/article/details/79120268 1.实战环境 windowns10: jdk1.8: springboo ...

  3. spring boot: 线程池ThreadPoolTaskExecutor, 多线程

    由于项目里需要用到线程池来提高处理速度,记录一下spring的taskExecutor执行器来实现线程池. ThreadPoolTaskExecutor的配置在网上找了很多解释没找到,看了下Threa ...

  4. spring boot 数据库连接池配置

    HikariCP 连接池配置: http://stackoverflow.com/questions/29650501/hikaricp-starts-when-mvn-spring-bootrun- ...

  5. Spring Boot中如何配置线程池拒绝策略,妥善处理好溢出的任务

    通过之前三篇关于Spring Boot异步任务实现的博文,我们分别学会了用@Async创建异步任务.为异步任务配置线程池.使用多个线程池隔离不同的异步任务.今天这篇,我们继续对上面的知识进行完善和优化 ...

  6. Spring线程池配置模板设计(基于Springboot)

    目录 线程池配置模板 基础的注解解释 常用配置参数 配置类设计 线程池使用 ThreadPoolTaskExecutor源码 线程池配置模板 springboot给我们提供了一个线程池的实现,它的底层 ...

  7. 【转】spring boot application.properties 配置参数详情

    multipart multipart.enabled 开启上传支持(默认:true) multipart.file-size-threshold: 大于该值的文件会被写到磁盘上 multipart. ...

  8. Spring Boot Quartz 动态配置,持久化

    Quartz 是一个很强大的任务调度框架在SpringBoot中也很容易集成 添加依赖: <dependency> <groupId>org.springframework&l ...

  9. SpringBoot异步使用@Async原理及线程池配置

    前言 在实际项目开发中很多业务场景需要使用异步去完成,比如消息通知,日志记录,等非常常用的都可以通过异步去执行,提高效率,那么在Spring框架中应该如何去使用异步呢 使用步骤 完成异步操作一般有两种 ...

随机推荐

  1. 【luogu P2002 消息扩散】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2002 缩点把原图变为DAG,再在DAG上判断找入度为0的点的个数. 注意一点出度为0的点的个数不等于入度为0 ...

  2. C# 动态改变webservice的访问地址

    1.添加一个App.config配置文件. 2.配置服务http://Lenovo-PC:80/EvisaWS/WharfService?wsdl,那么在上面的文件中就会自动生成服务的配置: < ...

  3. Swift_控制流

    Swift_控制流 点击查看源码 for-in 循环 //for-in 循环 fileprivate func testForIn() { //直接循环提取内部数据 //[1,5] for index ...

  4. Qt基于model/view数据库编程3

    QSqlQueryModel和QSqlQuery类: 工程开发过程中将这两个类合起来使用,用QSqlQueryModel查询展示数据库中的数据,用QSqlQuery类执行sql语言,实现对数据库的操作 ...

  5. Flask中对MySQL的基本操作

    在Flask-SQLAlchemy中,插入.修改.删除操作,均由数据库会话管理. 会话用 db.session 表示.在准备把数据写入数据库前,要先将数据添加到会话中然后调用 commit() 方法提 ...

  6. JavaScript实现图片切换

    页面内容:一个按钮标签  一个Img标签 实现原理:通过修改Img标签的src属性,实现图片的切换 备注:代码中flag变量仅仅用作标记,也可以直接用Img标签的src属性进行判断,不过在判断时候不能 ...

  7. js继承的几种方法和es6继承方法

        一.原型链继     1.基本思想     利用原型链来实现继承,超类的一个实例作为子类的原型     2.具体实现     function F() {}     //原型属性,原型方法: ...

  8. 【nginx下对服务器脚本php的支持】

    安装php7     下载地址:https://secure.php.net/downloads.php这里下载的是:wget http://ar2.php.net/distributions/php ...

  9. vue渲染自定义json数据

    <template> <div class="wrap"> <div class="main"> <div class ...

  10. Git----使用WebHook实现代码自动部署

    起因: 经常本地push到gitee等线上代码仓库,然后登陆服务器在进行pull,很麻烦,想偷懒怎么办?使用git的webhook实现! 1.实现原理 1.1本地提交推送 1.2线上仓库监听push动 ...