springboot创建线程池的两种方式小结
作者:若未 时间:2022-07-12 09:27:19
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<Runnable>(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注解,程序启动时创建
@Configuration
public 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<Runnable>(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")
来源:https://blog.csdn.net/qq_34204599/article/details/106013204
标签:springboot,线程池,创建
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java类加载机制实现流程及原理详解
2022-05-26 02:53:24
![](https://img.aspxhome.com/file/2023/4/75674_0s.png)
C#实现控制线程池最大数并发线程
2023-02-23 14:50:43
![](https://img.aspxhome.com/file/2023/5/119595_0s.png)
专属于程序员的浪漫-Java输出动态闪图iloveyou
2023-12-01 16:00:57
![](https://img.aspxhome.com/file/2023/8/84608_0s.png)
C#实现合并及拆分PDF文件的方法
2021-06-15 12:55:23
![](https://img.aspxhome.com/file/2023/2/119532_0s.jpg)
java图片验证码生成教程详解
2021-11-04 13:22:14
![](https://img.aspxhome.com/file/2023/7/60987_0s.jpg)
springboot 参数格式校验操作
2023-10-16 08:53:32
![](https://img.aspxhome.com/file/2023/5/110855_0s.png)
java中 ${} 和 #{} 有什么区别
2023-11-29 01:34:32
![](https://img.aspxhome.com/file/2023/5/67015_0s.png)
Java算法之递归算法计算阶乘
2021-06-30 14:10:56
![](https://img.aspxhome.com/file/2023/4/63454_0s.png)
Java多线程下解决数据安全问题
2022-05-16 04:48:45
Unity3D实现人物转向与移动
2022-10-28 08:42:13
android商户扫码枪读取手机二维码
2023-11-09 12:58:24
SpringBoot整合Shiro的代码详解
2023-10-30 10:53:31
Android编程开发之TextView单击链接弹出Activity的方法
2023-08-06 18:27:11
![](https://img.aspxhome.com/file/2023/4/83544_0s.png)
Android 高仿微信语音聊天页面高斯模糊(毛玻璃效果)
2021-09-27 18:56:46
![](https://img.aspxhome.com/file/2023/0/120160_0s.png)
Windows 10上JDK环境安装配置图文教程
2023-05-31 19:38:03
![](https://img.aspxhome.com/file/2023/7/86737_0s.png)
SpringCloud之@FeignClient()注解的使用方式
2022-05-16 04:22:40
![](https://img.aspxhome.com/file/2023/9/82979_0s.jpg)
Android唤醒、解锁屏幕代码实例
2022-09-03 14:11:51
Android中的LeakCanary的原理详解
2021-06-18 09:16:40
![](https://img.aspxhome.com/file/2023/0/100620_0s.png)
举例分析Python中设计模式之外观模式的运用
2021-11-24 01:54:48
详解Android中Handler的使用方法
2023-05-03 06:35:30
![](https://img.aspxhome.com/file/2023/8/101858_0s.png)