使用Spring ThreadPoolTaskExecutor实现多线程任务
我们为何使用多线程,之前已经有讲过了,为了更快的处理多个任务,分割任务,或者调用多个毫无关联的第三方服务
其实spring就提供了ThreadPoolTaskExecutor这个类来实现线程池,线程池是啥,可以理解为数据源,或者有一堆线程的池子也行
在spring配置中我们可以写好如下代码(大致意思都在注释中,不多说了,百度也一堆):
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<!-- 核心线程数 -->
<property name="corePoolSize" value="5" />
<!-- 最大线程数 -->
<property name="maxPoolSize" value="10" />
<!-- 队列最大长度 >=mainExecutor.maxSize -->
<property name="queueCapacity" value="25" />
<!-- 线程池维护线程所允许的空闲时间 -->
<property name="keepAliveSeconds" value="3000" />
<!-- 线程池对拒绝任务(无线程可用)的处理策略 ThreadPoolExecutor.CallerRunsPolicy策略 ,调用者的线程会执行该任务,如果执行器已关闭,则丢弃. -->
<property name="rejectedExecutionHandler">
<bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />
</property>
</bean>
然后定义一个component组件,然后线程的引用就十分简单了,只要把这个线程扔进这个线程池子就行了
@Component
public class FileCutter { @Autowired
private TaskExecutor taskExecutor; public void filesMng(String path, String fileName) {
this.taskExecutor.execute(new CutFilesThread(path,fileName));
} private class CutFilesThread implements Runnable {
private String path;
private String fileName;
private CutFilesThread(String path, String fileName) {
super();
this.path = path;
this.fileName = fileName;
}
@Override
public void run() {
System.out.println("barry... run...");
// display(path, fileName);
}
}
最后在你所需要的地方就可以调用这个组件了,不论是service还是controller都行
@Autowired
private FileCutter fileCutter; @RequestMapping("/cut")
@ResponseBody
public Object cut(){
fileCutter.filesMng("your path", "your fileName");
return "success";
}
使用Spring ThreadPoolTaskExecutor实现多线程任务的更多相关文章
- JavaEE开发之Spring中的多线程编程以及任务定时器详解
上篇博客我们详细的聊了Spring中的事件的发送和监听,也就是常说的广播或者通知一类的东西,详情请移步于<JavaEE开发之Spring中的事件发送与监听以及使用@Profile进行环境切换&g ...
- Spring ThreadPoolTaskExecutor队列满的异常处理
<!-- 配置线程池 --> <bean id="threadPool" class="org.springframework.scheduling.c ...
- Spring ThreadPoolTaskExecutor
1. ThreadPoolTaskExecutor配置 1 <!-- spring thread pool executor --> 2 <bean id="taskExe ...
- Spring Boot实践——多线程
多线程 Spring通过任务执行器(TaskExecutor)来实现多线程和并发编程.使用ThreadPoolTaskExecutor可实现一个基于线程池的TaskExecutor.而实际开发中任务一 ...
- Spring Boot 定时+多线程执行
Spring Boot 定时任务有多种实现方式,我在一个微型项目中通过注解方式执行定时任务. 具体执行的任务,通过多线程方式执行,单线程执行需要1小时的任务,多线程下5分钟就完成了. 执行效率提升10 ...
- spring中的多线程aop方法拦截
日常开发中,常用spring的aop机制来拦截方法,记点日志.执行结果.方法执行时间啥的,很是方便,比如下面这样:(以spring-boot项目为例) 一.先定义一个Aspect import org ...
- spring下的多线程
链接 1,http://haidaoqi3630.iteye.com/blog/1920944 2,http://www.importnew.com/27440.html .............. ...
- spring quartz使用多线程并发“陷阱”
定义一个job:ranJob,设置每秒执行一次,设置不允许覆盖并发执行 <bean id="rankJob" class="com.chinacache.www.l ...
- spring ThreadPoolTaskExecutor使用总结
ThreadPoolTaskExecutor提供TaskDecorator可以实现类似ThreadPoolExecutor.afterExecute()类似功能 taskDecorator主要是对Ru ...
随机推荐
- 安全工具acunetix使用
今天来主要介绍了安全测试工具AWVS(acunetix web vulnerability scanner)的使用 1) 安装包的下载地址:https://github.com/jiyanjiao/ ...
- macOS 上编译 Dynamips
Dynamips 是一个Cisco 路由器模拟软件. 安装过程: git clone git://github.com/GNS3/dynamips.git cd dynamips mkdir buil ...
- 【BZOJ5498】[十二省联考2019]皮配(动态规划)
[BZOJ5498][十二省联考2019]皮配(动态规划) 题面 BZOJ 洛谷 题解 先考虑暴力\(dp\),设\(f[i][j][k]\)表示前\(i\)所学校,有\(j\)人在某个阵营,有\(k ...
- consul命令记录
服务端启动脚本 #/bin/bash! echo "********************************************************************* ...
- hdu 1848 简单SG函数
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1848 Problem Description 任何一个大学生对菲波那契数列(Fibonacci num ...
- 第七篇--ubuntu18.04下面特殊符号
按住键盘win键,在搜索框输入“字符”,弹出来的工具点进去 需要什么符号就找什么符号,然后点击它复制就好.
- 使用mysqlbinlog对主库binlog进行同步
#!/bin/bash BASEDIR="/usr/local/mysql" BIN="$BASEDIR/bin" MYSQLBINLOG="$BIN ...
- Python常用模块-时间模块
在写代码的过程中,我们常常需要与时间打交道,在python中,与时间处理有关的模块有time,datetime和calendar.,这里主要介绍time和datetime模块 在python中,表示时 ...
- 类型和原生函数及类型转换(三:终结js类型转换)
Number() parseInt() parseFloat() Boolean() String() toString() 一.显式类型转换 -------Number()函数把对象的值转换为数字. ...
- NOI-OJ 2.2 ID:8758 2的幂次方表示
思路 可以把任意一个数转化为2^a+2^b+2^c+...+2^n 例如137的二进制为10001001,这就等效于2^7+2^3+2^0 以上结果如何通过程序循环处理呢?需要把数字n分解为上述公式, ...