> 文章列表 > SpringBoot+@Async注解

SpringBoot+@Async注解

SpringBoot+@Async注解

下面通过一个简单示例来直观的理解什么是同步调用:
定义Task类,创建三个处理函数分别模拟三个执行任务的操作,操作消耗时间随机取(10秒内)

@Component
public class Task {public static Random random =new Random();public void doTaskOne() throws Exception {System.out.println("开始做任务一");long start = System.currentTimeMillis();Thread.sleep(random.nextInt(10000));long end = System.currentTimeMillis();System.out.println("完成任务一,耗时:" + (end - start) + "毫秒");}public void doTaskTwo() throws Exception {System.out.println("开始做任务二");long start = System.currentTimeMillis();Thread.sleep(random.nextInt(10000));long end = System.currentTimeMillis();System.out.println("完成任务二,耗时:" + (end - start) + "毫秒");}public void doTaskThree() throws Exception {System.out.println("开始做任务三");long start = System.currentTimeMillis();Thread.sleep(random.nextInt(10000));long end = System.currentTimeMillis();System.out.println("完成任务三,耗时:" + (end - start) + "毫秒");}
}

在单元测试用例中,注入Task对象,并在测试用例中执行doTaskOne、doTaskTwo、doTaskThree三个函数。

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class ApplicationTests {@Autowiredprivate Task task;@Testpublic void test() throws Exception {task.doTaskOne();task.doTaskTwo();task.doTaskThree();}
}
开始做任务一
完成任务一,耗时:4256毫秒
开始做任务二
完成任务二,耗时:4957毫秒
开始做任务三
完成任务三,耗时:7173毫秒

上述的同步调用虽然顺利的执行完了三个任务,但是可以看到执行时间比较长,
若这三个任务本身之间不存在依赖关系,可以并发执行的话,同步调用在执行效率方面就比较差,可以考虑通过异步调用的方式来并发执行。
在Spring Boot中,我们只需要通过使用@Async注解就能简单的将原来的同步函数变为异步函数,Task类改在为如下模式:

@Component
public class Task {public static Random random =new Random();@Asyncpublic void doTaskOne() throws Exception {System.out.println("开始做任务一");long start = System.currentTimeMillis();Thread.sleep(random.nextInt(10000));long end = System.currentTimeMillis();System.out.println("完成任务一,耗时:" + (end - start) + "毫秒");}@Asyncpublic void doTaskTwo() throws Exception {System.out.println("开始做任务二");long start = System.currentTimeMillis();Thread.sleep(random.nextInt(10000));long end = System.currentTimeMillis();System.out.println("完成任务二,耗时:" + (end - start) + "毫秒");}@Asyncpublic void doTaskThree() throws Exception {System.out.println("开始做任务三");long start = System.currentTimeMillis();Thread.sleep(random.nextInt(10000));long end = System.currentTimeMillis();System.out.println("完成任务三,耗时:" + (end - start) + "毫秒");}
}

为了让@Async注解能够生效,还需要在Spring Boot的主程序中配置@EnableAsync,如下所示:

@SpringBootApplication
@EnableAsync
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}

此时可以反复执行单元测试,您可能会遇到各种不同的结果,比如:
没有任何任务相关的输出
有部分任务相关的输出
乱序的任务相关的输出
原因是目前doTaskOne、doTaskTwo、doTaskThree三个函数的时候已经是异步执行了。
主程序在异步调用之后,主程序并不会理会这三个函数是否执行完成了,
由于没有其他需要执行的内容,所以程序就自动结束了,导致了不完整或是没有输出任务相关内容的情况。

我们如何判断上述三个异步调用是否已经执行完成呢?
我们需要使用Future来返回异步调用的结果,就像如下方式改造doTaskOne函数:

@Component
public class Task {public static Random random =new Random();@Asyncpublic Future<String> doTaskOne() throws Exception {System.out.println("开始做任务一");long start = System.currentTimeMillis();Thread.sleep(random.nextInt(10000));long end = System.currentTimeMillis();System.out.println("完成任务一,耗时:" + (end - start) + "毫秒");return new AsyncResult<>("任务一完成");}@Asyncpublic Future<String> doTaskTwo() throws Exception {System.out.println("开始做任务二");long start = System.currentTimeMillis();Thread.sleep(random.nextInt(10000));long end = System.currentTimeMillis();System.out.println("完成任务二,耗时:" + (end - start) + "毫秒");return new AsyncResult<>("任务二完成");}@Asyncpublic Future<String> doTaskThree() throws Exception {System.out.println("开始做任务三");long start = System.currentTimeMillis();Thread.sleep(random.nextInt(10000));long end = System.currentTimeMillis();System.out.println("完成任务三,耗时:" + (end - start) + "毫秒");return new AsyncResult<>("任务三完成");}
}

按照如上方式改造一下其他两个异步函数之后,
下面我们改造一下测试用例,让测试在等待完成三个异步调用之后来做一些其他事情。

@Test
public void test() throws Exception {long start = System.currentTimeMillis();Future<String> task1 = task.doTaskOne();Future<String> task2 = task.doTaskTwo();Future<String> task3 = task.doTaskThree();while(true) {if(task1.isDone() && task2.isDone() && task3.isDone()) {// 三个任务都调用完成,退出循环等待break;}Thread.sleep(1000);}long end = System.currentTimeMillis();System.out.println("任务全部完成,总耗时:" + (end - start) + "毫秒");
}
开始做任务一
开始做任务二
开始做任务三
完成任务三,耗时:37毫秒
完成任务二,耗时:3661毫秒
完成任务一,耗时:7149毫秒
任务全部完成,总耗时:8025毫秒