启动类:添加@EnableAsync注解

@SpringBootApplication

@EnableAsync

public class Application{

  public static void main(String[] args) {

    SpringApplication.run(Application.class, args);

  }

}

Controller类

@RequestMapping("")

@RestController
public class AsyncTaskController {

  @Autowired
  private AsyncTask asyncTask;

  @RequestMapping("")
  public String doTask() throws InterruptedException{
    long currentTimeMillis = System.currentTimeMillis();
    Future<String> task1 = asyncTask.task1();

    Future<String> task2 = asyncTask.task2();
    Future<String> task3 = asyncTask.task3();
    String result = null;
    for (;;) {
      if(task1.isDone() && task2.isDone() && task3.isDone()) {
      // 三个任务都调用完成,退出循环等待
      break;
      }
      Thread.sleep(1000);
    }

    long currentTimeMillis1 = System.currentTimeMillis();

    result = "task任务总耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms";
    return result;

  }
}

异步任务类
@Component
public class AsyncTask {

  @Async
  public Future<String> task1() throws InterruptedException{
    long currentTimeMillis = System.currentTimeMillis();
    Thread.sleep(1000);
    long currentTimeMillis1 = System.currentTimeMillis();
    System.out.println("task1任务耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");

    return new AsyncResult<String>("task1执行完毕");
  }

  @Async
  public Future<String> task2() throws InterruptedException{
    long currentTimeMillis = System.currentTimeMillis();
    Thread.sleep(2000);
    long currentTimeMillis1 = System.currentTimeMillis();
    System.out.println("task2任务耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");

     return new AsyncResult<String>("task2执行完毕");
  }

  @Async
  public Future<String> task3() throws InterruptedException{
    long currentTimeMillis = System.currentTimeMillis();
    Thread.sleep(3000);
    long currentTimeMillis1 = System.currentTimeMillis();
    System.out.println("task3任务耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");

      return new AsyncResult<String>("task3执行完毕");
  }

}

结果:

  1. task1任务耗时:1000ms
  2.  task2任务耗时:2001ms
  3.  task3任务耗时:3001ms

  task任务总耗时:4015ms

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

  1. springboot异步线程(二)

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

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

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

  3. springboot异步线程

    前言 最近项目中出现了一个问题,发现自己的定时器任务在线上没有执行,但是在线下测试时却能执行,最后谷歌到了这篇文章SpringBoot踩坑日记-定时任务不定时了?; 本篇文章主要以自己在项目中遇到的问 ...

  4. SpringBoot学习笔记(七):SpringBoot使用AOP统一处理请求日志、SpringBoot定时任务@Scheduled、SpringBoot异步调用Async、自定义参数

    SpringBoot使用AOP统一处理请求日志 这里就提到了我们Spring当中的AOP,也就是面向切面编程,今天我们使用AOP去对我们的所有请求进行一个统一处理.首先在pom.xml中引入我们需要的 ...

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

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

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

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

  7. 卸载AppDomain动态调用DLL异步线程执行失败

    应用场景 动态调用DLL中的类,执行类的方法实现业务插件功能 使用Assembly 来实现 但是会出现逻辑线程数异常的问题 使用AppDomain 实现动态调用,并卸载. 发现问题某个插件中开启异步线 ...

  8. c#异步线程:同步调用,异步调用,异步回调

    定义一个异步线程类: public class AsyEventClass { private static ILog logger = LogManager.GetLogger(MethodBase ...

  9. SpringBoot异步调用--@Async详解

    1. 概述   在日常开发中,为了提高主线程的效率,往往需要采用异步调用处理,例如系统日志等.在实际业务场景中,可以使用消息中间件如RabbitMQ.RocketMQ.Kafka等来解决.假如对高可用 ...

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

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

随机推荐

  1. flutter textfield设置高度后内容区无法居中?

    textfiled 设置高度后,内容永远无法居中,最后找到原因 decoration: 中有一个 contentPadding属性,设置这个属性对应的Padding值即可

  2. CSV Data Set Config参数说明

    以下是CSV Data Set Config各个参数的简要说明: FileName:即同目录下csv文件的名称 File Encoding: 默认为ANSI Varible Names: 定义文本文件 ...

  3. LoadRunner中的Web 函数列表

    LoadRunner中的Web 函数列表 web test LoadRunner fuction_list D:\Program Files (x86)\Mercury Interactive\Mer ...

  4. IOS input框轻点无效修复方法

    FastClick.prototype.focus = function(targetElement) { targetElement.focus();//加入这一句话就OK了 };

  5. Could not resolve host: mirrorlist.centos.org Centos 7 Unkown error

    安装Centos7(core)以后,网卡默认不会启用.这是一个大坑,直接报错,这是一个过度优化,有几个开发人员/运维人员安装centos7(core)不用ssh去连接服务器的. 报错如下: Loade ...

  6. 30 道linux运维题

    1.linux如何挂在windows下的共享目录mount.cifs //192.168.1.3/server /mnt/server -o user=administrator,pass=12345 ...

  7. SQL中group by使用

    多条重复记录,有一个字段可以进行区分,从重复的记录中取出其中一条,可利用group by字段 举例如下: 查询所有 查询地区中的最高分

  8. Linux中编译C文件

    C/C++程序编译的过程 预处理,展开头文件,宏定义,条件编译处理等.通过gcc -E source.c -o source.i或者cpp source.c生成. 编译.这里是一个狭义的编译意义,指的 ...

  9. Structured streaming: A Declarative API for Real-Time Applications in Apache Spark(Abstract: 原文+注译)

    题目中文:结构化流: Apache spark中,处理实时数据的声明式API Abstract with the ubiquity of real-time data, organizations n ...

  10. Windows 下关于转码的函数

    std::string& MsgFieldList::GBToUTF8(std::string& des,const char* str) { WCHAR *strSrc; TCHAR ...