springboot如何创建线程池
发表于:2025-11-16 作者:千家信息网编辑
千家信息网最后更新 2025年11月16日,这篇文章主要介绍springboot如何创建线程池,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!springboot创建线程池两种方式1.使用static代码块创建这样的方式创
千家信息网最后更新 2025年11月16日springboot如何创建线程池
这篇文章主要介绍springboot如何创建线程池,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
springboot创建线程池两种方式
1.使用static代码块创建
这样的方式创建的好处是当代码用到线程池的时候才会初始化核心线程数
具体代码如下:
public class HttpApiThreadPool { /** 获取当前系统的CPU 数目*/ static int cpuNums = Runtime.getRuntime().availableProcessors(); /** 线程池核心池的大小*/ private static int corePoolSize = 10; /** 线程池的最大线程数*/ private static int maximumPoolSize = cpuNums * 5; public static ExecutorService httpApiThreadPool = null; /** * 静态方法 */ static{ System.out.println("创建线程数:"+corePoolSize+",最大线程数:"+maximumPoolSize); //建立10个核心线程,线程请求个数超过20,则进入队列等待 httpApiThreadPool = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, 0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(100),new ThreadFactoryBuilder().setNameFormat("PROS-%d").build()); } } 使用方法
public static void main(String[] args) { HttpApiThreadPool.httpApiThreadPool.execute(()->System.out.println("测试")); }注意:
1.不能使用Executors的方法创建线程池,这个是大量的生产事故得出来的结论
2.maximumPoolSize本程序使用的是cup数的5倍,你可以看你实际情况用
3.new ThreadFactoryBuilder().setNameFormat("PROS-%d").build() 给每个线程已名字,可以方便调试
2.使用@Configuration @bean注解,程序启动时创建
@Configurationpublic class TreadPoolConfig { private Logger logger = LoggerFactory.getLogger(TreadPoolConfig.class); /** 获取当前系统的CPU 数目*/ int cpuNums = Runtime.getRuntime().availableProcessors(); /** 线程池核心池的大小*/ private int corePoolSize = 10; /** 线程池的最大线程数*/ private int maximumPoolSize = cpuNums * 5; /** * 消费队列线程 * @return */ @Bean(value = "httpApiThreadPool") public ExecutorService buildHttpApiThreadPool(){ logger.info("TreadPoolConfig创建线程数:"+corePoolSize+",最大线程数:"+maximumPoolSize); ExecutorService pool = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, 0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(100),new ThreadFactoryBuilder().setNameFormat("PROS-%d").build()); return pool ; } } 使用方法
//注入 @Resource private TreadPoolConfig treadPoolConfig; //调用 public void test() { treadPoolConfig.buildHttpApiThreadPool().execute(()->System.out.println("tre")); }现在两种线程池的创建方法已经介绍完了。
springboot开启线程池
定义线程池
定义的位置,要在启动类的子包或者同级目录中
import lombok.Data;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.ThreadPoolExecutor;@Data@Configuration@EnableAsync //开启异步请求public class ThreadPoolConfig { private static final int corePoolSize = 10; // 核心线程数(默认线程数) private static final int maxPoolSize = 100; // 最大线程数 private static final int keepAliveTime = 10; // 允许线程空闲时间(单位:默认为秒) private static final int queueCapacity = 500; // 缓冲队列数 /** * 默认异步线程池 * @return */ @Bean("taskExecutor") public ThreadPoolTaskExecutor taskExecutor(){ ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor(); pool.setThreadNamePrefix("--------------全局线程池-----------------"); pool.setCorePoolSize(corePoolSize); pool.setMaxPoolSize(maxPoolSize); pool.setKeepAliveSeconds(keepAliveTime); pool.setQueueCapacity(queueCapacity); // 直接在execute方法的调用线程中运行 pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 初始化 pool.initialize(); return pool; }}使用
直接在需要异步执行的方法上加注解
@Async("taskExecutor")以上是"springboot如何创建线程池"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!
线程
方法
最大
核心
代码
队列
使用方法
内容
大小
数目
方式
注解
程序
篇文章
系统
个数
事故
价值
位置
全局
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
机架式服务器机柜安装
南宁广科网络技术
国家网络安全法的核心
华为5g网络安全性
软件开发创业公司压力好大
云测网络安全管理平台
软件开发游戏难不难
云服务器不能运行管家婆
杭州百世网络技术怎么样
咪咕动漫软件开发支撑方
网络技术 公司
网络安全态势的分析报告
服务器安全警示标语
镇江网络安全工作招聘
软件开发啥也不会
计算机网络技术第六版边倩
互联网时代科技赋能教育
BIOS升级软件开发
怎么完善数据库资料
网络安全扫描主要有哪些类型
软件开发 网络技术怎么概述
2021网络安全宣传周山东
cmd怎么导入本地数据库
物理层软件开发工程师翻译
我的是世界怎么创造服务器
公司级关系型数据库
网络安全法中的防范风险措施
西安软件开发行情
统计局网络安全风险评估
黄浦区互联网络技术信息