文章目录
- 1.模拟业务方法 并添加注解@Async
- 2.在启动类增加@EnableAsync注解
- 3.测试类与接口一
- 4.结果比较
- 5.使用AsyncResult接收异步执行结果
- 6.测试类与接口二
- 7.总结
1.模拟业务方法 并添加注解@Async
@Service
public class TaskService {
private static final Random random = new Random();
@Async
public void task1() {
System.out.println("run task1 start...");
long start = System.currentTimeMillis();
try {
Thread.sleep(random.nextInt(10000));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("run task1 end..., time: " + (System.currentTimeMillis() - start) + " ms");
}
@Async
public void task2() {
System.out.println("run task2 start...");
long start = System.currentTimeMillis();
try {
Thread.sleep(random.nextInt(10000));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("run task2 end..., time: " + (System.currentTimeMillis() - start) + " ms");
}
@Async
public void task3() {
System.out.println("run task3 start...");
long start = System.currentTimeMillis();
try {
Thread.sleep(random.nextInt(10000));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("run task3 end..., time: " + (System.currentTimeMillis() - start) + " ms");
}
}
2.在启动类增加@EnableAsync注解
@SpringBootApplication
@EnableAsync
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
3.测试类与接口一
@RestController
public class TestAsyncController {
@Autowired
private TaskService taskService;
@GetMapping("test/async/task")
public void testAsyncTask() {
long start = System.currentTimeMillis();
taskService.task1();
taskService.task2();
taskService.task3();
System.out.println("run all task end: " + (System.currentTimeMillis() - start) + " ms");
}
}
4.结果比较
// task任务不加 @Async 的日志打印
run task1 start...
run task1 end..., time: 3739 ms
run task2 start...
run task2 end..., time: 8025 ms
run task3 start...
run task3 end..., time: 9057 ms
run all task end: 20821 ms
// task任务加 @Async 的日志打印 主任务先结束
run all task end: 4 ms
run task1 start...
run task2 start...
run task3 start...
run task3 end..., time: 1095 ms
run task2 end..., time: 4952 ms
run task1 end..., time: 8442 ms
5.使用AsyncResult接收异步执行结果
@Service
public class TaskService {
private static final Random random = new Random();
@Async
public Future<String> task1() {
System.out.println("run task1 start...");
long start = System.currentTimeMillis();
try {
Thread.sleep(random.nextInt(10000));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("run task1 end..., time: " + (System.currentTimeMillis() - start) + " ms");
return new AsyncResult<>("task1 result");
}
@Async
public Future<String> task2() {
System.out.println("run task2 start...");
long start = System.currentTimeMillis();
try {
Thread.sleep(random.nextInt(10000));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("run task2 end..., time: " + (System.currentTimeMillis() - start) + " ms");
return new AsyncResult<>("task2 result");
}
@Async
public Future<String> task3() {
System.out.println("run task3 start...");
long start = System.currentTimeMillis();
try {
Thread.sleep(random.nextInt(10000));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("run task3 end..., time: " + (System.currentTimeMillis() - start) + " ms");
return new AsyncResult<>("task3 result");
}
}
6.测试类与接口二
@RestController
public class TestAsyncController {
@Autowired
private TaskService taskService;
@GetMapping("test/async/task")
public void testAsyncTask() {
long start = System.currentTimeMillis();
Future<String> future1 = taskService.task1();
Future<String> future2 = taskService.task2();
Future<String> future3 = taskService.task3();
while (true) {
if (future1.isDone() && future2.isDone() && future3.isDone()) {
System.out.println(future1.get());
System.out.println(future2.get());
System.out.println(future3.get());
break;
}
TimeUnit.MICROSECONDS.sleep(500);
}
System.out.println("run all task end: " + (System.currentTimeMillis() - start) + " ms");
}
}
7.总结
- 使用@Async注解的方法task1 task2 task3是异步调用的
- 使用@Async在主任务先结束,但主任务需要获取task1 task2 task3的执行结果的场景中不适用,需要配合AsyncResult实现(查看AsyncResult的源码,会发现它实现了
Future<T>
) - @Async所修饰的函数不要定义为static类型,这样异步调用不会生效
还没有评论,来说两句吧...