JDK线程池和Spring线程池的使用实例解析
作者:晒太阳的兔子 时间:2023-02-24 13:27:32
这篇文章主要介绍了JDK线程池和Spring线程池的使用实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
JDK线程池和Spring线程池实例,异步调用,可以直接使用
(1)JDK线程池的使用,此处采用单例的方式提供,见示例:
public class ThreadPoolUtil {
private static int corePoolSize = 5;
private static int maximumPoolSize = 10;
private static long keepAliveTime = 60L;
private static TimeUnit unit = TimeUnit.SECONDS;
private static int capacity = 1024;
private static ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("jdk-thread-pool-%d").build();
private static final ExecutorService executorService = new ThreadPoolExecutor(corePoolSize,
maximumPoolSize,
keepAliveTime,
unit,
new LinkedBlockingQueue<>(capacity),
namedThreadFactory,
new ThreadPoolExecutor.AbortPolicy());
private ThreadPoolUtil () {
}
public static ExecutorService getExecutorService () {
return executorService;
}
}
在其它地方可以直接这样使用:
ThreadPoolUtil.getExecutorService().execute(() -> {
System.out.println("test1");
System.out.println("test2");
}
)
(2)Spring线程池的使用,此处通过配置类的方式配置线程池的相关属性,见示例:
@Configuration
@EnableAsync
public class DocataThreadBeanConfig {
private int corePoolSize = 5;
private int maxPoolSize = 10;
private int queueCapacity = 1024;
private String namePrefix = "async-service-task-";
// 上述属性可以通过@Value来读取配置值
@Bean(name = "asyncServiceTaskExecutor")
public TaskExecutor asyncServiceExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 设置核心线程数
executor.setCorePoolSize(corePoolSize);
// 设置最大线程数
executor.setMaxPoolSize(maxPoolSize);
// 设置队列容量
executor.setQueueCapacity(queueCapacity);
// 设置线程活跃时间(秒)
executor.setKeepAliveSeconds(60);
// 设置默认线程名称
executor.setThreadNamePrefix(namePrefix);
// 设置拒绝策略
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 等待所有任务结束后再关闭线程池
executor.setWaitForTasksToCompleteOnShutdown(true);
executor.initialize();
;
return executor;
}
}
在其它文件中需要这样使用:
@Resource(name="asyncServiceTaskExecutor")
private ThreadPoolTaskExecutor asyncServiceTaskExecutor;
不要直接使用@Autowired,否则会提示失败的
@Autowired
private ThreadPoolTaskExecutor asyncServiceTaskExecutor;
来源:https://www.cnblogs.com/bien94/p/11954310.html
标签:jdk,线程池,spring
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
unity实现玻璃效果
2023-03-17 07:08:28
![](https://img.aspxhome.com/file/2023/8/116598_0s.jpg)
Java实现学生管理系统(控制台版本)
2023-04-11 04:12:05
Mybatis + js 实现下拉列表二级联动效果
2022-09-14 03:43:06
![](https://img.aspxhome.com/file/2023/9/69699_0s.png)
Android虚拟机Dalvik和ART科普
2022-12-31 06:02:28
![](https://img.aspxhome.com/file/2023/9/138799_0s.png)
SpringBoot集成Swagger构建api文档的操作
2023-11-03 09:13:51
![](https://img.aspxhome.com/file/2023/4/62384_0s.jpg)
C#实现上位机与欧姆龙PLC通讯(FINS)
2022-10-05 03:10:08
Android开发中解析xml文件XmlUtils工具类与用法示例
2023-03-26 01:05:45
Kotlin中的5种单例模式示例详解
2022-03-01 22:51:58
![](https://img.aspxhome.com/file/2023/6/139446_0s.jpg)
SpringBoot中使用Session共享实现分布式部署的示例代码
2022-10-17 04:27:54
![](https://img.aspxhome.com/file/2023/7/66687_0s.png)
C#如何动态创建lambda表达式
2022-04-18 21:57:16
c#反射调用方法示例
2022-07-14 15:28:40
Spring中自定义数据类型转换的方法详解
2022-10-09 02:56:51
c# 预处理识别硬币的数据集
2022-05-21 17:26:09
C#中 MessageBox的使用技巧
2023-06-25 16:15:48
![](https://img.aspxhome.com/file/2023/2/85682_0s.webp)
java算法之Math.random()随机概率玩法实例演示
2023-11-28 23:32:17
Java ConcurrentHashMap用法案例详解
2023-08-30 02:01:41
基于Java Springboot + Vue + MyBatis实现音乐播放系统
2023-07-09 16:01:41
![](https://img.aspxhome.com/file/2023/1/106131_0s.png)
Android百度地图之方向感应和模式更改
2021-10-08 20:38:39
![](https://img.aspxhome.com/file/2023/8/139308_0s.jpg)
java中利用List的subList方法实现对List分页(简单易学)
2022-06-18 23:33:09
Java8中关于Function.identity()的使用
2021-11-16 16:05:15
![](https://img.aspxhome.com/file/2023/1/67161_0s.png)