今天在这里学习下使用springboot的异步调用async

首先使用@EnableAsync开启异步功能

/**
* @author fengzp
* @date 17/5/8
* @email fengzp@gzyitop.com
* @company 广州易站通计算机科技有限公司
*/
@SpringBootApplication
@EnableAsync
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
} }

测试类

    /**
* @author fengzp
* @date 17/5/8
* @email fengzp@gzyitop.com
* @company 广州易站通计算机科技有限公司
*/
@Component
public class AsyncTest { public static Random random =new Random(); /**
* @Async所修饰的函数不要定义为static类型,否则异步调用不会生效
*
* 这里通过返回Future<T>来返回异步调用的结果,实现异步回调
*/
@Async
public Future<String> test1() throws InterruptedException {
System.out.println("test1 begin");
long begin = System.currentTimeMillis();
Thread.sleep(random.nextInt(10000));
System.out.println("test1 end " + (System.currentTimeMillis() - begin));
return new AsyncResult<String>("test1 is done!");
} @Async
public Future<String> test2() throws InterruptedException {
System.out.println("test2 begin");
long begin = System.currentTimeMillis();
Thread.sleep(random.nextInt(10000));
System.out.println("test2 end " + (System.currentTimeMillis() - begin));
return new AsyncResult<String>("test2 is done!");
} @Async
public Future<String> test3() throws InterruptedException {
System.out.println("test3 begin");
long begin = System.currentTimeMillis();
Thread.sleep(random.nextInt(10000));
System.out.println("test3 end " + (System.currentTimeMillis() - begin));
return new AsyncResult<String>("test3 is done!");
}
}

测试

/**
* @author fengzp
* @date 17/5/8
* @email fengzp@gzyitop.com
* @company 广州易站通计算机科技有限公司
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class Test { @Autowired
AsyncTest asyncTest; @org.junit.Test
public void test() throws InterruptedException {
System.out.println("begin");
long begin = System.currentTimeMillis();
Future<String> test1 = asyncTest.test1();
Future<String> test2 = asyncTest.test2();
Future<String> test3 = asyncTest.test3(); while(true) {
if(test1.isDone() && test2.isDone() && test3.isDone())
break; Thread.sleep(500);
} System.out.println("end 耗时: " + (System.currentTimeMillis() - begin));
}
}

运行结果

spring boot实现异步调用的更多相关文章

  1. Spring Boot 异步方法的调用

    Spring Boot 异步方法的调用 参考资料: 1.Spring Boot中使用@Async实现异步调用 使用方法 两个步骤: 1.开启配置 @EnableAsync,这一步特别容易忘记,导致测试 ...

  2. Spring boot 配置异步处理执行器

    示例如下: 1. 新建Maven 项目 async-executor 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0 ...

  3. Spring @Async实现异步调用示例

    什么是“异步调用”? “异步调用”对应的是“同步调用”,同步调用指程序按照定义顺序依次执行,每一行程序都必须等待上一行程序执行完成之后才能执行:异步调用指程序在顺序执行时,不等待异步调用的语句返回结果 ...

  4. Spring Boot Async异步执行

    异步调用就是不用等待结果的返回就执行后面的逻辑,同步调用则需要等带结果再执行后面的逻辑. 通常我们使用异步操作都会去创建一个线程执行一段逻辑,然后把这个线程丢到线程池中去执行,代码如下: Execut ...

  5. 17、Spring Boot普通类调用bean【从零开始学Spring Boot】

    转载:http://blog.csdn.net/linxingliang/article/details/52013017 我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个 ...

  6. Spring Boot普通类调用bean

    1 在Spring Boot可以扫描的包下 假设我们编写的工具类为SpringUtil. 如果我们编写的SpringUtil在Spring Boot可以扫描的包下或者使用@ComponentScan引 ...

  7. Spring Boot @Async 异步任务执行

    1.任务执行和调度 Spring用TaskExecutor和TaskScheduler接口提供了异步执行和调度任务的抽象. Spring的TaskExecutor和java.util.concurre ...

  8. Spring Boot发布和调用RESTful web service

    Spring Boot可以非常简单的发布和调用RESTful web service,下面参考官方指导体验一下 1.首先访问 http://start.spring.io/ 生成Spring Boot ...

  9. (17)Spring Boot普通类调用bean【从零开始学Spring Boot】

    我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,想直接使用 ...

随机推荐

  1. 链栈的基本操作(C语言)

    栈的链式储存结构称为链栈.链栈的节点类型与链式线性表的节点类型 定义相同,不同的是它是仅在表头进行操作的单链表.链栈通常用不带头节 点的单链表来实现,栈顶指针就是链表的头指针 ,如图所示: 代码如下: ...

  2. 【转】使用PHP导入和导出CSV文件

    项目开发中,很多时候要将外部CSV文件导入到数据库中或者将数据导出为CSV文件,那么具体该如何实现呢?本文将使用PHP并结合mysql,实现了CSV格式数据的导入和导出功能.我们先准备mysql数据表 ...

  3. php socket通过smtp发送邮件(纯文本、HTML,多收件人,多抄送,多密送)

    <?php /** * 邮件发送类 * 支持发送纯文本邮件和HTML格式的邮件,可以多收件人,多抄送,多秘密抄送 * @example * $mail = new MySendMail(); * ...

  4. 2018.12.12 codeforces 935D. Fafa and Ancient Alphabet(概率dp)

    传送门 概率dp水题. 题意简述:给你数字表的大小和两个数列,数列中为0的数表示不确定,不为0的表示确定的,求第一个数列字典序比第二个数列大的概率. fif_ifi​表示第i ni~ ni n位第一个 ...

  5. java Concurrent包学习笔记(七):ConcurrentHashMap

    (注意:以下讲解的ConcurrentHashMap是jdk 1.8的) 一.ConcurrentHashMap的数据结构 ConcurrentHashMap在1.8中的实现,相比于1.7的版本基本上 ...

  6. C++ 中 new 操作符内幕:new operator、operator new、placement new

    一.new 操作符(new operator) 人们有时好像喜欢有益使C++语言的术语难以理解.比方说new操作符(new operator)和operator new的差别. 当你写这种代码: st ...

  7. 强大的DataGrid组件[1]

    说明:DataGrid组件是Silverlight数据组件中最为常用并且是功能最为强大的数据组件.因此,对开发者而言,深入了解其特性是十分有必要的.本文先介绍该组件的基本特性,接着通过几个简单实例来说 ...

  8. (打表+优化)简单的求和 -- zzuli -- 1783

    http://acm.zzuli.edu.cn/problem.php?id=1783 1783: 简单的求和 Time Limit: 1 Sec  Memory Limit: 128 MBSubmi ...

  9. GPT分区在IBM服务器上安装linux不能引导的解决方法

    提示: Your boot partition is on a disk using the GPT partitioning Scheme but this machines cannot boot ...

  10. Java并发编程:线程池的使用(转载)

    文章出处:http://www.cnblogs.com/dolphin0520/p/3932921.html Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实 ...