前言

在本篇文章中,我们主要讨论spring异步编程的一些相关知识,不涉及实战。springboot版本2.2.1

TaskExecutor

spring2.0后提出TaskExecutor接口,作为任务执行者抽象。TaskExecutor源码:


@FunctionalInterface
public interface TaskExecutor extends Executor {
@Override
void execute(Runnable task); }

spring框架提供了一定的TaskExecutor实现类,这些实现类可以完成几乎所有使用场景的覆盖,所以,大多数情况下,我们没有必要实现某个TaskExecutor

  1. SyncTaskExecutor

    代码如下:

public class SyncTaskExecutor implements TaskExecutor, Serializable {
@Override
public void execute(Runnable task) {
Assert.notNull(task, "Runnable must not be null");
task.run();
} }

可以发现,提交给SyncTaskExecutor的任务都是直接在当前线程中执行

  1. 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的任务每次都新建一个线程来执行提交的任务。

  1. 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);
}
}
  1. ConcurrentTaskExecutor

    ConcurrentTaskExecutorJava5ExecutorspringTaskExecutor搭建了一道桥梁使得我们可以将Executor框架下的某些实现类以TaskExecutor的形式公开来,如果我们感觉ThreadPoolTaskExecutor封装的java.util.concurrent.ThreadPoolExecutor不足以満足当前场景需要,那么可以构建需要的Executor实例,比如通过Executors.newXXXThreadPool(),然后以ConcurrentTaskExecutor对其进行封装,封装后获得的ConcurrentTaskExecutor即获得相应Executor的能力,但它现在是以TaskExecutor的样子示人气如下所示:

Executor executor =Executors .newScheduledThreadPool (10);
TaskExecutor taskExecutor = new ConcurrentTaskExecutor (executor):

最后

异步线程的一些相关知识知道了。接下来就是怎么去使用了。

参考:

  1. Spring Boot Async Task Executor
  2. Spring 官方文档
  3. 新手也能看懂的 SpringBoot 异步编程指南
  4. TaskExecutionAutoConfiguration

springboot异步线程的更多相关文章

  1. springboot异步线程(二)

    前言 本篇文章针对上篇文章springboot异步线程,有一位大佬在评论中提出第一点是错误的,当时看到了这个问题,最近刚好有空,针对第一点的问题去搜索了不少的文章: 问题 我在文章中第一点去验证:Sc ...

  2. SpringBoot 异步线程简单三种样式

    引用:在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的:但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务,其实,在Spring 3.x ...

  3. SpringBoot使用异步线程池实现生产环境批量数据推送

    前言 SpringBoot使用异步线程池: 1.编写线程池配置类,自定义一个线程池: 2.定义一个异步服务: 3.使用@Async注解指向定义的线程池: 这里以我工作中使用过的一个案例来做描述,我所在 ...

  4. springboot 中如何正确在异步线程中使用request

    起因: 有后端同事反馈在异步线程中获取了request中的参数,然后下一个请求是get请求的话,发现会偶尔出现参数丢失的问题. 示例代码: @GetMapping("/getParams&q ...

  5. SpringBoot异步使用@Async原理及线程池配置

    前言 在实际项目开发中很多业务场景需要使用异步去完成,比如消息通知,日志记录,等非常常用的都可以通过异步去执行,提高效率,那么在Spring框架中应该如何去使用异步呢 使用步骤 完成异步操作一般有两种 ...

  6. SpringBoot 自定义线程池

    本教程目录: 自定义线程池 配置spring默认的线程池 1. 自定义线程池 1.1 修改application.properties task.pool.corePoolSize=20 task.p ...

  7. 新手也能看懂的 SpringBoot 异步编程指南

    本文已经收录自 springboot-guide : https://github.com/Snailclimb/springboot-guide (Spring Boot 核心知识点整理. 基于 S ...

  8. 转载-SpringBoot结合线程池解决多线程问题实录;以及自己的总结

    原文地址:https://blog.csdn.net/GFJ0814/article/details/92422245 看看这篇文章(继续学习):https://www.jianshu.com/p/3 ...

  9. SpringBoot异步编程

    异步调用:当我们执行一个方法时,假如这个方法中有多个耗时的任务需要同时去做,而且又不着急等待这个结果时可以让客户端立即返回然后,后台慢慢去计算任务.当然你也可以选择等这些任务都执行完了,再返回给客户端 ...

随机推荐

  1. LeetCode 1062. Longest Repeating Substring

    原题链接在这里:https://leetcode.com/problems/longest-repeating-substring/ 题目: Given a string S, find out th ...

  2. 使用mustache 做为docker容器运行动态配置工具

    很多时候我们需要在启动容器的时候基于配置文件运行,如果在配置比较简单的时候我们可以通过环境变量 注入,同时当前12 factors 越来越融入大家的开发中了(对于配置通过环境变量处理),但是好多老的软 ...

  3. call JSON.parse JSON.stringify typeof 的使用及严格模式this的使用

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  4. Sublime Text 3关闭自动升级提醒

    由于种种原因,导致不想升级现有版本的ST3,但是被它的升级提醒弹窗严重骚扰! ||||||||||| 解 决 办 法 ||||||||||| 1.首选项 - 设置 - 用户(快捷键 ❀,)打开“Pre ...

  5. 使用gdb调试段错误

    [https://blog.csdn.net/xj9120/article/details/91380074] 1.bt 2.frame number 3.一般是内存问题 4.kill

  6. 行车记录仪 MyCar Recorder (转)

    行车记录仪 MyCar Recorder

  7. mybatis如何接受map类型的参数

    Mybatis传入参数类型为Map   mybatis更新sql语句: ? 1 2 3 4 5 6 7 8 9 <update id="publishT00_notice" ...

  8. 解决 No IDEA annotations attached to the JDK 1.8和xml文件没有代码提示

    Android studio3.3 用着用着突然xml里没有代码联想了,忙着做其他的就没管,写xml的时候就硬写... 然后今天用着突然在class文件上方提示No IDEA annotations ...

  9. Mybatis出错: Cause: org.apache.ibatis.reflection.ReflectionException: Error instantiating class com.cyf.pojo.User with invalid types () or values ()

    org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: org.apache.ib ...

  10. Cassandra开发入门文档第五部分(使用场景)

    正确建模 开发人员在构建Cassandra数据库时犯的另一个主要错误是分区键的选择不佳.cassandra是分布式的.这意味着您需要有一种方法来跨节点分布数据.Cassandra通过散列每个表的主键( ...