今天在这里学习下使用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. windows 10 开发学习资料,Windows-universal-samples学习笔记系列一:App settings

    windows 10 通用代码范例: https://github.com/Microsoft/Windows-universal-samples 相关视频:https://mix.office.co ...

  2. regular exception

    RegexBuddy :正则表达式编辑器. 正则表达式:通过特定的规则,选取特定的字符串. ^ 为匹配输入字符串的开始位置.$ 为匹配输入字符串的结束位置. \d匹配[0-9] \w匹配[A-Z a- ...

  3. oracle 大量连接导致数据库不能登录

    系统遇到过几次这种问题,一个系统申请的session数过大,导致数据库进程数满,无法连接的问题. pl sql develope 报的错误是:ORA-12170:TNS:链接超时 oracle用户登录 ...

  4. Eclipse中配置约束

    一.本地配置schema约束(xsd文件): 1.比如配置spring的applicationContext.xml中的约束条件: 复制applicationContext.xml中如图: 2.win ...

  5. mac os 下 Android Studio设置真机调试

    http://www.cnblogs.com/developer-wang/p/6719555.html 如果没有 .bash_profile 只需要创建 .bash_profile,然后增加andr ...

  6. c#多线程编程实战(原书第二版)文摘

    Thread t = new Thread(PrintNumbersWithDelay); t.Start(); t.Join(); 但我们在主程序中调用了t.Join方法,该方法允许我们等待直到线程 ...

  7. GDI基础(3):绘制图片

    1.CBitmap位图类封装了Windows GDI中的位图和操作位图的成员函数.CPen.CBrush.CFont.CBitmap是常用的Windows GDI对象,和CFont一样,CBitmap ...

  8. etf基金和lof基金区别

    ①,含义不同.etf即交易指数开放基金,是跟踪某一指数的可以在交易所上市的开放式基金.lof基金是上市向开放基金,是中国首创的一种基金类型,也是etf基金的中国化.②,申购赎回的场所不同.etf和lo ...

  9. usb_ctrl

    IFCLK应该是clkout,与数据同步 flagd就是就是slcs FPGA的系统时钟用FX2LP的clkout时钟,个人觉得关键点在时钟是48M,别的异步时钟也是可以的. 模式配置 使用的模式是从 ...

  10. MFC源码实现文件对照表

    CDocManager类[实现文件] /SRC/DOCTEMPL.CPP CSingleDocTemplate类[实现文件] /SRC/DOCSINGL.CPP CWinApp::OnFileOpen ...