一、概述
针对一些方法之间没有相互依赖关系的调用,我们通常可以使用异步调用,这样可以减少方法执行的时间,针对SpringBoot,我们可以使用@Async实现异步调用的功能。
二、示例演示
package com.liutao.component;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.util.Random;
/**
* 演示@Async实现异步调用
*
* @author LIUTAO
* @version 2017/5/9
* @see
* @since
*/
@Component
public class Task {
private static Random random = new Random();
@Async
public void doTaskOne() throws InterruptedException {
System.out.println("begin task-one");
long startTime = System.currentTimeMillis();
Thread.sleep(random.nextInt(1000));
long endTime = System.currentTimeMillis();
System.out.println("the time of completing task-one :"+(endTime-startTime));
}
@Async
public void doTaskTwo() throws InterruptedException {
System.out.println("begin task-two");
long startTime = System.currentTimeMillis();
Thread.sleep(random.nextInt(1000));
long endTime = System.currentTimeMillis();
System.out.println("the time of completing task-two :"+(endTime-startTime));
}
@Async
public void doTaskThree() throws InterruptedException {
System.out.println("begin task-three");
long startTime = System.currentTimeMillis();
Thread.sleep(random.nextInt(1000));
long endTime = System.currentTimeMillis();
System.out.println("the time of completing task-three :"+(endTime-startTime));
}
}
package com.liutao.component;
import com.liutao.application.Application;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* 测试@Async标签功能
*
* @author LIUTAO
* @version 2017/5/9
* @see
* @since
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class AsyncTest {
@Autowired
private Task task;
@Test
public void test() throws InterruptedException {
task.doTaskOne();
task.doTaskTwo();
task.doTaskThree();
}
}
package com.liutao.application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
/**
* 程序启动主类
*
* @author LIUTAO
* @version 2017/3/29
* @see
* @since
*/
@SpringBootApplication
@EnableAsync
@ComponentScan(basePackages={"com.liutao"})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
注意:针对上面的示例,我们使用@Async注解相应的方法,使方法调用采用异步的方式,同时注意要在应用启动类上添加@EnableAsync标签。但是这样使用的情况下,我们无法获得异步方法执行的结果,如果我们想要取得异步方法执行的结果,又应该如何处理呢?
package com.liutao.component;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;
import java.util.Random;
import java.util.concurrent.Future;
/**
* 演示@Async实现异步调用
*
* @author LIUTAO
* @version 2017/5/9
* @see
* @since
*/
@Component
public class Task {
private static Random random = new Random();
@Async
public Future<String> doTaskOne() throws InterruptedException {
System.out.println("begin task-one");
long startTime = System.currentTimeMillis();
Thread.sleep(random.nextInt(1000));
long endTime = System.currentTimeMillis();
System.out.println("the time of completing task-one :"+(endTime-startTime));
return new AsyncResult<>("task-one finish");
}
@Async
public Future<String> doTaskTwo() throws InterruptedException {
System.out.println("begin task-two");
long startTime = System.currentTimeMillis();
Thread.sleep(random.nextInt(1000));
long endTime = System.currentTimeMillis();
System.out.println("the time of completing task-two :"+(endTime-startTime));
return new AsyncResult<>("task-two finish");
}
@Async
public Future<String> doTaskThree() throws InterruptedException {
System.out.println("begin task-three");
long startTime = System.currentTimeMillis();
Thread.sleep(random.nextInt(1000));
long endTime = System.currentTimeMillis();
System.out.println("the time of completing task-three :"+(endTime-startTime));
return new AsyncResult<>("task-three finish");
}
}
package com.liutao.component;
import com.liutao.application.Application;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.concurrent.Future;
/**
* 测试@Async标签功能
*
* @author LIUTAO
* @version 2017/5/9
* @see
* @since
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class AsyncTest {
@Autowired
private Task task;
@Test
public void test() throws InterruptedException {
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("the time of completing all task :"+(end-start));
}
}
执行结果:
begin task-two
begin task-one
begin task-three
the time of completing task-two :444
the time of completing task-three :715
the time of completing task-one :857
the time of completing all task :1000
可以看见我们通过让异步方法返回Future<String>就可以实现获取返回结果。