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;
------20191128闪
JDK线程池和Spring线程池的使用的更多相关文章
- spring线程池(同步、异步)
一.spring异步线程池类图 二.简单介绍 2.1. TaskExecutor---Spring异步线程池的接口类,其实质是java.util.concurrent.Executor 以下是官方已经 ...
- Spring线程池配置模板设计(基于Springboot)
目录 线程池配置模板 基础的注解解释 常用配置参数 配置类设计 线程池使用 ThreadPoolTaskExecutor源码 线程池配置模板 springboot给我们提供了一个线程池的实现,它的底层 ...
- spring线程池ThreadPoolTaskExecutor与阻塞队列BlockingQueue
一: ThreadPoolTaskExecutor是一个spring的线程池技术,查看代码可以看到这样一个字段: private ThreadPoolExecutor threadPoolExecut ...
- java和spring 线程池总结
1. spring 的线程池 ThreadPoolTaskExecutor @Configuration public class ThreadPoolConfig { @Bean("thr ...
- 分享知识-快乐自己:Spring线程池配置
Spring通过ThreadPoolTaskExecutor实现线程池技术,它是使用jdk中的Java.util.concurrent.ThreadPoolExecutor进行实现. Spring 配 ...
- spring线程池的同步和异步(1)
spring线程池(同步.异步) 一.spring异步线程池类图 二.简单介绍 2.1. TaskExecutor---Spring异步线程池的接口类,其实质是java.util.concurrent ...
- 007-多线程-JUC线程池-Spring线程池配置、池子如何配置参数
一.概述 Spring通过ThreadPoolTaskExecutor实现线程池技术,它是使用jdk中的Java.util.concurrent.ThreadPoolExecutor进行实现. 1.1 ...
- spring线程池配置
源自:http://zjriso.iteye.com/blog/771706 1.了解 TaskExecutor接口 Spring的TaskExecutor接口等同于java.util.concurr ...
- Spring线程池开发实战
Spring线程池开发实战 作者:chszs,转载需注明. 作者博客主页:http://blog.csdn.net/chszs 本文提供了三个Spring多线程开发的例子,由浅入深,由于例子一目了然, ...
随机推荐
- 杭电-------2032杨辉三角(C语言写)
#include<stdio.h> ][] = { }; void init() { int i, j; ; i < ; i++) { a[i][] = ; a[i][i] = ; ...
- Mysql性能优化全揭秘-庖丁解牛
「为什么写」 一直想写数据库相关的文章,最直接的原因是数据库这块我们工作中每天都会用到,也是面试求职绕不开的话题,无论你是何种测试,优秀的数据库能力都会非常加分,最近我在总结数据库性能优化这块内容,性 ...
- CCS过渡和动画
过渡 过渡能让使用过渡的元素在样式发生变化时(例如鼠标划过,单击按钮,点击图片时,颜色,尺寸,位置等样式发生变化),定义变化过程中的动画,让变化不再是瞬间产生. 过渡样式使用transition定义, ...
- python3-cookbook笔记:第五章 文件与IO
python3-cookbook中每个小节以问题.解决方案和讨论三个部分探讨了Python3在某类问题中的最优解决方式,或者说是探讨Python3本身的数据结构.函数.类等特性在某类问题上如何更好地使 ...
- sass实现头条新闻列表页面
Index.html <!DOCTYPE html> <html> <head> <title>今日头条</title> <meta ...
- docker配置搭建gogs
参考文献: https://www.yeboyzq.com/linux/ruanjiananzhuangweihu/1012.html https://www.jianshu.com/p/d92fd4 ...
- Wannafly Winter Camp 2020 Day 6H 异或询问 - 二分
给定一个长 \(n\) 的序列 \(a_1,\dots,a_n\),定义 \(f(x)\) 为有多少个 \(a_i \leq x\) 有 \(q\) 次询问,每次给定 \(l,r,x\),求 \(\s ...
- git文件冲突合并的报错:Your local changes to the following files would be overwritten by merge
记录一下在项目里使用git遇到代码冲突时的解决方法 问题:当我和我同事两个人改了相同的一个文件,他在我提交前提交了,这时候我就提交不了了,并且也pull不下来他的代码 会报错: Your local ...
- while语句的使用
学习while语句的正确用法:题目:老师教学生,讲一次之后 问学生会不会,如果不会:就再讲一遍.如果会了就放学,但是如果连讲了十遍还是不会,那也要放学回家 namespace _44讲十遍会不会 { ...
- Java连载86-List集合详解
一.List集合 1.List集合存储元素的特点: (1)有序(List集合中存储有下标):存进去是这样的顺序,取出来还是按照这个顺序取出. (2)可重复 2.深入ListJ集合 ArrayLis ...