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异步编程
异步调用:当我们执行一个方法时,假如这个方法中有多个耗时的任务需要同时去做,而且又不着急等待这个结果时可以让客户端立即返回然后,后台慢慢去计算任务.当然你也可以选择等这些任务都执行完了,再返回给客户端 ...
随机推荐
- date -d
date -d ‘2 days ago’ //显示2天以前的时间date -d ‘60 second ago’ //显示60秒以前的时间 date -d '3 months 1 day' //显示3月 ...
- 5-微信小程序开发(小程序页面跳转和布局说明)
https://www.cnblogs.com/yangfengwu/p/11605209.html 新建一个小程序 咱现在新建个页面 在pages 上右击,选择新建目录 会自动添加这几个文件 现在做 ...
- A@G!C005
AGC005 A STring 不会,有没有老鸽蕉蕉我/kk/kel/dk https://agc005.contest.atcoder.jp/submissions/7926986 B Minimu ...
- GitHub 手把手教你如何把本地项目或代码提交到Github托管
GitHub 手把手教你如何把项目或代码提交到Github托管 启动Git Bash命令行 重点内容 1.首先打开你的github,点击新建项目,点击new repositories ,然后直接给项目 ...
- SpringMVC效验器
效验器: 1. 效验器依赖 <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator --> &l ...
- Review of Semantic Segmentation with Deep Learning
In this post, I review the literature on semantic segmentation. Most research on semantic segmentati ...
- 【BigData】Java基础_数组
什么是数组?数据是可以装一组数据的变量 1.定义数组 float[] arr1 = new float[10]; // 可以装10个float数据 int[] arr2 = new int[10]; ...
- js函数如何传递多个参数
应用场景: 需要根据多个参数来判断该数据所属,从而达到删除或者修改的目的. 比如删除区域下的分组,一个区域可以用多个分组,不同的区域可以有相同的分组,那么如何识别对应的分组呢??? 可以在对应的数据操 ...
- Http的状态码及状态码的类型
Http的状态码以及根据这些状态码分成5种类型 statusCode/100 /* * Copyright 2002-2013 the original author or authors. * * ...
- Salt States概览
作者言 我也只是SaltStack的初学者,如果文中有错误的地方,请不吝赐教. 在学习的过程,我也做了不少实验,犯了不少错,积累了一些经验,对SaltStack的运行也有一定了解,如果有什么问题,或是 ...