步骤,如图所示:

1.添加异步任务业务类

package top.ytheng.demo.task;import java.util.concurrent.Future;import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;//异步任务业务类
@Component
//标记此类是异步类,也可在方法中标记
//不加,则类里面的方法为同步执行
@Async
public class AsyncTask {public void task1() throws InterruptedException {long begin = System.currentTimeMillis();Thread.sleep(1000);long end = System.currentTimeMillis();System.out.println("任务1耗时:" + (end - begin));}public void task2() throws InterruptedException {long begin = System.currentTimeMillis();Thread.sleep(2000);long end = System.currentTimeMillis();System.out.println("任务2耗时:" + (end - begin));}public void task3() throws InterruptedException {long begin = System.currentTimeMillis();Thread.sleep(3000);long end = System.currentTimeMillis();System.out.println("任务3耗时:" + (end - begin));}//测试拿到返回结果public Future<String> task4() throws InterruptedException {long begin = System.currentTimeMillis();Thread.sleep(1000);long end = System.currentTimeMillis();System.out.println("任务4耗时:" + (end - begin));return new AsyncResult<String>("任务4");}public Future<String> task5() throws InterruptedException {long begin = System.currentTimeMillis();Thread.sleep(2000);long end = System.currentTimeMillis();System.out.println("任务5耗时:" + (end - begin));return new AsyncResult<String>("任务5");}public Future<String> task6() throws InterruptedException {long begin = System.currentTimeMillis();Thread.sleep(3000);long end = System.currentTimeMillis();System.out.println("任务6耗时:" + (end - begin));return new AsyncResult<String>("任务6");}
}

2.添加测试控制器

package top.ytheng.demo.controller;import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import top.ytheng.demo.task.AsyncTask;@RestController
@RequestMapping("api/v1/async")
public class TaskController {@Autowiredprivate AsyncTask asyncTask;@GetMapping("/test")public Object test() throws InterruptedException, ExecutionException {long begin = System.currentTimeMillis();//asyncTask.task1();//asyncTask.task2();//asyncTask.task3();Future<String> result1 = asyncTask.task4();Future<String> result2 = asyncTask.task5();Future<String> result3 = asyncTask.task6();System.out.println("返回结果:" + result1.get() + "," + result2.get() + "," + result3.get());for(;;) {if(result1.isDone() && result2.isDone() && result3.isDone()) {break;}}long end = System.currentTimeMillis();long total = end - begin;System.out.println("总耗时:" + total);return "总耗时:" + total;}
}

3.添加启动类

package top.ytheng.demo;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;@SpringBootApplication //等于下面3个
//@SpringBootConfiguration
//@EnableAutoConfiguration
//@ComponentScan
//拦截器用到
@ServletComponentScan
//MyBatis用到
@MapperScan("top.ytheng.demo.mapper")
//定时使用(开启定时任务)
@EnableScheduling
//开启异步任务
@EnableAsync
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
}

4.右键项目Run As启动,访问url

http://localhost:8080/api/v1/async/test

结果:

SpringBoot------异步任务的使用相关推荐

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

    通过本文你可以了解到下面这些知识点: Future 模式介绍以及核心思想 核心线程数.最大线程数的区别,队列容量代表什么: ThreadPoolTaskExecutor饱和策略: SpringBoot ...

  2. SpringBoot异步调用方法

    SpringBoot异步调用方法 一.spring boot--使用异步请求,提高系统的吞吐量 https://blog.csdn.net/liuchuanhong1/article/details/ ...

  3. Springboot异步多线程编程

    文章目录 一.基础知识 二.什么时候用同步&异步 三.什么时候需要使用多线程 四.springboot异步多线程编程实现 一.基础知识 同步:同步就是指一个进程在执行某个请求的时候,若该请求需 ...

  4. springboot异步注解_SpringBoot如何实现异步、定时任务?

    (一)异步任务 异步任务的需求在实际开发场景中经常遇到,Java实现异步的方式有很多,比如多线程实现异步.在SpringBoot中,实现异步任务只需要增加两个注解就可以实现.当前类添加@Async注解 ...

  5. SpringBoot异步任务, 以及带返回值的异步任务(@Async 不起作用的原因)

    第一部分: 无返回值异步任务 当没有加入异步任务的时候,我们创建一个service ,里面的方法需要等待3秒才能完成, controller层写一个测试方法调用时间返回的接口, 直接调用, 下面是se ...

  6. springboot异步和切面_Spring异步编程 你的@Async就真的异步吗?异步历险奇遇记

    引言有点长 前端的宝宝会用ajax,用异步编程到快乐的不行~ 我们java也有异步,用起来比他们还快乐~ 我们biaji一个注(gǒupí)解(gāoyào),也是快乐风男... 且看下面的栗子: 注 ...

  7. springboot异步和切面_Spring异步编程 | 你的@Async就真的异步吗 ☞ 异步历险奇遇记...

    引言有点长 前端的宝宝会用ajax,用异步编程到快乐的不行~ 我们java也有异步,用起来比他们还快乐~ 我们bia~ji~一个注(gǒupí)解(gāoyào),也是快乐风男... 且看下面的栗子: ...

  8. springboot异步和切面_Spring异步编程 | 你的@Async就真的异步吗?异步历险奇遇记

    Spring异步编程 | 你的@Async就真的异步吗?异步历险奇遇记 点击上方"java进阶架构师",选择右上角"置顶公众号" 20大进阶架构专题每日送达 引 ...

  9. springboot异步和切面_SpringBoot强化篇(八)-- Spring AOP

    Spring AOP简介 AOP(Aspect Orient Programming)是一种设计思想,是软件设计领域中的面向切面编程,它是面向对象编程(OOP)的一种补充和完善.它以通过预编译方式和运 ...

  10. springboot异步任务带自定义返回结果和异步任务查看。

    前言:由于多年在写的都是python,经常在做一些异步任务时,如导入导出报表这种,都是用celery来做异步生成表格,然后循环更新任务状态,任务结束后返回文件名或其他结果.最近某个项目采用java的s ...

最新文章

  1. Anaconda3 离线安装和配置 Django-3.2.7 使用 MySQL-5.7 数据库
  2. zerodivisionerror什么意思python-python代码里出现是啥意思
  3. 实用比较,帮你决策到底选择Vue还是Angular4、5
  4. python图片横向合并_[宜配屋]听图阁
  5. 多功能拼团商城源码-带优惠券功能+自适应移动端+对接免签约支付
  6. (转)goldengate 复制进程replicat出现ORA-01403 错误
  7. mysql char(36)_MySQL中char(36)被認為是GUID導致的BUG及解決方案
  8. CentOS 使用yum update 更新时保留特定版本的软件
  9. ARM、DSP、FPGA的区别
  10. dts双轨制会员积分系统
  11. 计算机网页加载失败如何解决方法,驱动程序加载失败的常见解决方案
  12. Alfred效率神器
  13. asp.net 获取网页源文件的方法
  14. JavaWeb - 小米商城网 - 项目启动
  15. ASP.NET MVC4 微信公众平台开发测试一: 验证
  16. 工程师也该学习机器学习了!
  17. xcopy 跳过已经存在的_视频课怎么区分数学一二三?考研英语怎么复习?恋练有词句子部分直接跳过?...
  18. 推荐几个高质量的程序员 B 站视频账号
  19. 原收件服务器地址 端口 协议,常用的收件、发件服裳组词务器的地址和端口是什么...
  20. 关联分析/频繁项集挖掘:Apriori算法

热门文章

  1. hdu 2602 Bone Collector 01背包
  2. MySQL数据导入oracle
  3. 使用LoadRunner测试WMS
  4. ubuntu 18.04设置系统自带系统截图快捷键
  5. Pycharm 导入 Python 包、模块
  6. 五、curator recipes之选举主节点Leader Latch
  7. 在VSCode中编写Kotlin/Java
  8. ES6/7 异步编程学习笔记
  9. layer iframe层的使用,传参
  10. (转)创建Windows服务(Windows Services)N种方式总结