使用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 ...
随机推荐
- UVA 10118 Free Candies
https://vjudge.net/problem/UVA-10118 题目 桌上有4堆糖果,每堆有$N$($N\leqslant 40$)颗.有个熊孩子拿了个可以装5颗糖的篮子,开始玩无聊的装糖游 ...
- Python之常见算法介绍
一.算法介绍 1. 算法是什么 算法是指解题方案的准确而完整的描述,是一系列解决问题的清晰指令,算法代表着用系统的方法描述解决问题的策略机制.也就是说,能够对一定规范的输入,在有限时间内获得所要求的输 ...
- magento 2 Check https://getcomposer.org/doc/articles/troubleshooting.md#memory-limit-errors for more info on how to handle out of memory errors.%
Check https://getcomposer.org/doc/articles/troubleshooting.md#memory-limit-errors for more info on h ...
- mysql配置记录
[mysqld] datadir=/app/data/mysql socket=/app/data/mysql/mysql.sock symbolic-links=0 validate_passwor ...
- 微信小程序之canvas 文字断行和省略号显示
文字的多行处理在dom元素中很好办.但是canvas中没有提供方法,只有通过截取指定字符串来达到目的. 那么下面就介绍我自己处理的办法: wxml: <canvas canvas-id='wor ...
- pandas的分布式执行框架之modin
Scale your pandas workflows by changing one line of code To use Modin, replace the pandas impo ...
- 13、 使用openpyxl存储周杰伦的歌曲信息
import requests import openpyxl res = requests.get('https://c.y.qq.com/soso/fcgi-bin/client_search ...
- SpringBoot系列: Spring支持的异常处理方式
===================================视图函数返回 status code 的方式===================================Spring 有 ...
- Java CAS 比较并且更换值
原文:Java中CAS详解 作者:jayxu无捷之径 在JDK 5之前Java语言是靠synchronized关键字保证同步的,这会导致有锁 锁机制存在以下问题: (1)在多线程竞争下,加锁.释放锁会 ...
- webpack学习笔记——publicPath路径问题
output: { filename: "[name].js", path:path.resolve(__dirname,"build") } 如果没有指定pu ...