1. 在程序入口加入开启异步标签
@EnableAsync
2. 在需要执行异步的类中加入标签 (工具类)
package com.example.utils;
import java.util.concurrent.Future;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;
@Component
public class AsyncUtil {
@Async
public Future<Boolean> doAsync1() throws Exception{
long start = System.currentTimeMillis();
Thread.sleep(1000);
long end = System.currentTimeMillis();
System.out.println("任务1耗时:"+ (end - start) + "毫秒");
return new AsyncResult<>(true);
}
@Async
public Future<Boolean> doAsync2() throws Exception{
long start = System.currentTimeMillis();
Thread.sle