Spring中用@Async注解标记的方法,称为异步方法
,它会在调用方的当前线程之外的独立的线程中执行,
new Thread(()-> System.out.println("hello world !"))
这样在另一个线程中去执行相应的业务逻辑异步方法实际的执行交给了 Spring 的 TaskExecutor 来完成。
调用异步方法类上需要配置上注解@EnableAsync,或者是在启动类上加上@EnableAsync
mode=AdviceMode.PROXY
),同一个类内部没有使用@Async注解修饰的方法调用@Async注解修饰的方法,是不会异步执行的
实现类内部自调用也可以异步
,则需要切换@EnableAsync注解的mode=AdviceMode.ASPECTJ
import org.springframework.scheduling.annotation.Async;public interface TestService {@Asyncvoid test();
}
import com.example.service.TestService;
import org.springframework.stereotype.Service;@Service
public class TestServiceImpl implements TestService {@Overridepublic void test() {//...}
}
调用异步方法的controller类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping(value = "/test")
@EnableAsync
public class TestContoller {@Autowiredprivate TestService testService;@GetMapping(value = "/testAsync")public void print() {testService.test();}
}
如果我们去除TestController上的@EnableAsync或者new 一个TestService对象(该对象没有加载进Spring的容器中),那么TestController中的print()方法都会同步执行,后台打印日志也可以看到只有一个线程在执行
如果启动类上配置@EnableAsync,那么在上例代码中,便不需要再在Controller上面加上@EnableAsync这个注解
入口类增加了 @EnableAsync 注解,主要是为了扫描范围包下的所有 @Async 注解。
@Async 修饰test()方法,test2()方法不修饰
public interface TestService {@Asyncvoid test();void test2();
}
public class TestServiceImpl implements TestService{@Overridepublic void test() {//...}@Overridepublic void test2() {test();//自调用test()方法}
}
这种情况下“:test2()方法调用的test()方法并没有异步执行,只有一个线程
Spring中提供了一个 Future 接口的子类:AsyncResult,所以我们可以返回 AsyncResult 类型的值。
AsyncResult是异步方式,异步主要用于调用的代码需要长时间运行,才能返回结果的时候,可以不阻塞调用者
public class AsyncResult implements ListenableFuture {private final V value;private final ExecutionException executionException;//...
}
AsyncResult实现了ListenableFuture接口,该对象内部有两个属性:返回值和异常信息。
public interface ListenableFuture extends Future {void addCallback(ListenableFutureCallback super T> var1);void addCallback(SuccessCallback super T> var1, FailureCallback var2);
}
public Future test() throws Exception {log.info("开始做任务");long start = System.currentTimeMillis();Thread.sleep(1000);long end = System.currentTimeMillis();log.info("完成任务,耗时:" + (end - start) + "毫秒");return new AsyncResult<>("任务完成,耗时" + (end - start) + "毫秒");
}
返回自定义类型的话,在上面的Future里面的String改为对应的实体类
统计一下三个任务并发执行共耗时多少,这就需要等到上述三个函数都完成调动之后记录时间,并计算结果。
也可以使用CompletableFuture来返回异步调用的结果
@Async
public CompletableFuture doTaskOne() throws Exception {log.info("开始做任务一");long start = System.currentTimeMillis();Thread.sleep(random.nextInt(10000));long end = System.currentTimeMillis();log.info("完成任务一,耗时:" + (end - start) + "毫秒");return CompletableFuture.completedFuture("任务一完成");
}
@Test
public void test() throws Exception {long start = System.currentTimeMillis();CompletableFuture task1 = asyncTasks.test();CompletableFuture task2 = asyncTasks.test();CompletableFuture task3 = asyncTasks.test();CompletableFuture.allOf(task1, task2, task3).join();long end = System.currentTimeMillis();log.info("任务全部完成,总耗时:" + (end - start) + "毫秒");
}
自行谷歌或百度,spring线程池配置,基本上很详细了