springboot异步线程
前言
在本篇文章中,我们主要讨论spring异步编程的一些相关知识,不涉及实战。springboot版本2.2.1
TaskExecutor
spring2.0后提出TaskExecutor
接口,作为任务执行者抽象。TaskExecutor
源码:
@FunctionalInterface
public interface TaskExecutor extends Executor {
@Override
void execute(Runnable task);
}
spring框架提供了一定的TaskExecutor
实现类,这些实现类可以完成几乎所有使用场景的覆盖,所以,大多数情况下,我们没有必要实现某个TaskExecutor
;
- SyncTaskExecutor
代码如下:
public class SyncTaskExecutor implements TaskExecutor, Serializable {
@Override
public void execute(Runnable task) {
Assert.notNull(task, "Runnable must not be null");
task.run();
}
}
可以发现,提交给SyncTaskExecutor
的任务都是直接在当前线程中执行
- SimpleAsyncTaskExecutor
@Override
public void execute(Runnable task, long startTimeout) {
Assert.notNull(task, "Runnable must not be null");
Runnable taskToUse = (this.taskDecorator != null ? this.taskDecorator.decorate(task) : task);
if (isThrottleActive() && startTimeout > TIMEOUT_IMMEDIATE) {
this.concurrencyThrottle.beforeAccess();
doExecute(new ConcurrencyThrottlingRunnable(taskToUse));
}
else {
doExecute(taskToUse);
}
}
protected void doExecute(Runnable task) {
Thread thread = (this.threadFactory != null ? this.threadFactory.newThread(task) : createThread(task));
thread.start();
}
提交给SimpleAsyncTaskExecutor
的任务每次都新建一个线程来执行提交的任务。
- ThreadPoolTaskExecutor
如果觉得SimpleAsyncTaskExecutor
每次都需要新建线程不可取,就可以使用这个,ThreadPoolTaskExecutor
改用线程池来管理并重用处理任务异步执行的工作线程。其中,ThreadPoolTaskExecutor
的线程池功能是使用的jdk的ThreadPoolExecutor
来实现的。
@Override
public void execute(Runnable task) {
Executor executor = getThreadPoolExecutor();
try {
executor.execute(task);
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
}
}
- ConcurrentTaskExecutor
ConcurrentTaskExecutor
为Java5
的Executor
和spring
的TaskExecutor
搭建了一道桥梁使得我们可以将Executor
框架下的某些实现类以TaskExecutor
的形式公开来,如果我们感觉ThreadPoolTaskExecutor
封装的java.util.concurrent.ThreadPoolExecutor
不足以満足当前场景需要,那么可以构建需要的Executor
实例,比如通过Executors.newXXXThreadPool()
,然后以ConcurrentTaskExecutor
对其进行封装,封装后获得的ConcurrentTaskExecutor
即获得相应Executor
的能力,但它现在是以TaskExecutor
的样子示人气如下所示:
Executor executor =Executors .newScheduledThreadPool (10);
TaskExecutor taskExecutor = new ConcurrentTaskExecutor (executor):
最后
异步线程的一些相关知识知道了。接下来就是怎么去使用了。
参考:
- Spring Boot Async Task Executor
- Spring 官方文档
- 新手也能看懂的 SpringBoot 异步编程指南
- TaskExecutionAutoConfiguration
springboot异步线程的更多相关文章
- springboot异步线程(二)
前言 本篇文章针对上篇文章springboot异步线程,有一位大佬在评论中提出第一点是错误的,当时看到了这个问题,最近刚好有空,针对第一点的问题去搜索了不少的文章: 问题 我在文章中第一点去验证:Sc ...
- SpringBoot 异步线程简单三种样式
引用:在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的:但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务,其实,在Spring 3.x ...
- SpringBoot使用异步线程池实现生产环境批量数据推送
前言 SpringBoot使用异步线程池: 1.编写线程池配置类,自定义一个线程池: 2.定义一个异步服务: 3.使用@Async注解指向定义的线程池: 这里以我工作中使用过的一个案例来做描述,我所在 ...
- springboot 中如何正确在异步线程中使用request
起因: 有后端同事反馈在异步线程中获取了request中的参数,然后下一个请求是get请求的话,发现会偶尔出现参数丢失的问题. 示例代码: @GetMapping("/getParams&q ...
- SpringBoot异步使用@Async原理及线程池配置
前言 在实际项目开发中很多业务场景需要使用异步去完成,比如消息通知,日志记录,等非常常用的都可以通过异步去执行,提高效率,那么在Spring框架中应该如何去使用异步呢 使用步骤 完成异步操作一般有两种 ...
- SpringBoot 自定义线程池
本教程目录: 自定义线程池 配置spring默认的线程池 1. 自定义线程池 1.1 修改application.properties task.pool.corePoolSize=20 task.p ...
- 新手也能看懂的 SpringBoot 异步编程指南
本文已经收录自 springboot-guide : https://github.com/Snailclimb/springboot-guide (Spring Boot 核心知识点整理. 基于 S ...
- 转载-SpringBoot结合线程池解决多线程问题实录;以及自己的总结
原文地址:https://blog.csdn.net/GFJ0814/article/details/92422245 看看这篇文章(继续学习):https://www.jianshu.com/p/3 ...
- SpringBoot异步编程
异步调用:当我们执行一个方法时,假如这个方法中有多个耗时的任务需要同时去做,而且又不着急等待这个结果时可以让客户端立即返回然后,后台慢慢去计算任务.当然你也可以选择等这些任务都执行完了,再返回给客户端 ...
随机推荐
- 洛谷 P2577 [ZJOI2005]午餐 题解
每日一题 day56 打卡 Analysis 算法:贪心+dp 容易想到贪心:吃饭慢的先打饭节约时间, 所以先将人按吃饭时间从大到小排序. 然后就是dp了: 首先,应该想到f[i][j][k]:前i个 ...
- firewall-cmd命令的富语言(richlanguage)示例
一.为协议“ah”启用新的IPv4和IPv6连接 firewall-cmd --permanent --add-rich-rule 'rule protocol value="ah" ...
- 4-网页,网站,微信公众号基础入门(配置网站--下载安装PHP)
https://www.cnblogs.com/yangfengwu/p/10979101.html 这一节咱看一下如何在原先的基础上实现网站 首先去下载 PHP https://windows.ph ...
- 动态规划-多维DP
1.最大正方形 我的瞎猜分析: 我的瞎猜算法: #include <stdio.h> #include <memory.h> #include <math.h> # ...
- 链表 | 递归删除不带头结点链表所有x元素
王道P37 T1 : 设计一个递归算法,删除不带头结点的单链表L中所有值为x的结点. 王道上的答案绝对是错的,我自己想了一个 函数主体 LinkList* del_x(LinkList* prior, ...
- nginx 访问控制之 限速
nginx限速可以通过 ngx_http_limit_conn_module 和 ngx_http_limit_req_module 模块来实现限速的功能. 一.ngx_http_limit_conn ...
- python 路径拼接
>>> import os>>> os.path.join('/hello/','good/boy/','doiido')>>>'/hello/g ...
- unicode欺骗—— hctf - admin
查看源代码,发现<!-- you are not admin --> 提示要以管理员身份登陆 尝试注册管理员账号,提示The username has been registered 于是 ...
- NULL与nullptr
[https://blog.csdn.net/weixin_40237626/article/details/82560012] 其实啊,在编译器进行解释程序时,NULL会被直接解释成0,所以这里的参 ...
- win10安装ubuntu双系统遇到的问题
安装过程学习了几个博客 Ubuntu 16.04与Win10双系统双硬盘安装图解:https://www.cnblogs.com/coxiseed/p/9945202.html?tdsourcetag ...