在
SpringBoot
应用中,经常会遇到在一个接口中,同时做事情1,事情2,事情3,如果同步执行的话,则本次接口时间取决于事情1 2 3执行时间之和;如果三件事同时执行,则本次接口时间取决于事情1 2 3执行时间最长的那个,合理使用多线程,可以大大缩短接口时间。
简介
@Async
注解在使用时,如果不指定线程池的名称,则使用Spring
默认的线程池,Spring
默认的线程池为SimpleAsyncTaskExecutor
。- 方法上一旦标记了这个
@Async
注解,当其它线程调用这个方法时,就会开启一个新的子线程去异步处理该业务逻辑。
默认线程池参数:
默认核心线程数:8,
最大线程数:Integet.MAX_VALUE,
队列使用LinkedBlockingQueue,
容量是:Integet.MAX_VALUE,
空闲线程保留时间:60s,
线程池拒绝策略:AbortPolicy。可以看到,默认线程池的大小最大线程数非常夸张,如果出现循环或者其他并发情况会无限制创建线程。所以一般 建议使用自定义线程池 。
一、快速使用
SpringBoot
应用中需要添加@EnableAsync
注解,来开启异步调用,一般还会配置一个线程池,异步的方法交给特定的线程池完成,如下:
@Configuration
@EnableAsync
public class AsyncConfiguration {
@Bean("firstExecutor")
public Executor doSomethingExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 核心线程数:线程池创建时候初始化的线程数
executor.setCorePoolSize(10);
// 最大线程数:线程池最大的线程数,只有在缓冲队列满了之后才会申请超过核心线程数的线程
executor.setMaxPoolSize(20);
// 缓冲队列:用来缓冲执行任务的队列
executor.setQueueCapacity(500);
// 允许线程的空闲时间60秒:当超过了核心线程之外的线程在空闲时间到达之后会被销毁
executor.setKeepAliveSeconds(60);
// 线程池名的前缀:设置好了之后可以方便我们定位处理任务所在的线程池
executor.setThreadNamePrefix("first-");
// 缓冲队列满了之后的拒绝策略:由调用线程处理(一般是主线程)
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
executor.initialize();
return executor;
}
}
使用的方式非常简单,在需要异步的方法上加@Async注解
@RestController
public class FirstController {
@Autowired
private IOneService oneService;
@GetMapping("/")
public void firstInterface() {
for (int i = 0; i < 10; i++) {
oneService.methodOne(i + "");
}
}
}
-------------------------------------------------------------------------------------------
@Slf4j
@Service
public class IOneServiceImpl implements IOneService {
// 指定使用beanname为firstExecutor的线程池
@Async("firstExecutor")
@Override
public void methodOne(String msg) {
log.info("first-executor:message{}", msg);
try {
Thread.sleep(5 * 1000);
} catch (InterruptedException e) {
log.error(e.getMessage());
}
}
}
二、获取异步方法返回值
当异步方法有返回值时,如何获取异步方法执行的返回结果呢?这时需要异步调用的方法带有返回值CompletableFuture。
CompletableFuture是对Feature的增强,Feature只能处理简单的异步任务,而CompletableFuture可以将多个异步任务进行复杂的组合。如下:
@RestController
public class AsyncController {
@Autowired
private AsyncService asyncService;
@GetMapping("/comp")
public void secondInterface() {
System.out.println("开始处理" + new Date());
CompletableFuture<String> future1 = oneService.methodOne("一");
CompletableFuture<String> future2 = oneService.methodTwo("二");
CompletableFuture<String> future3 = oneService.methodThree("三");
// 手动阻塞主线程,直至上述三个业务线程完成返回
CompletableFuture.allOf(future1, future2, future3);
// 当三个业务线程都成功完成后,才解除主线程的阻塞,抽取业务结果
System.out.println("手动阻塞后的非业务语句1");
try {
System.out.println("future1.isDone() = " + future1.isDone());
System.out.println(future1.get());
System.out.println("手动阻塞后的非业务语句2");
System.out.println("future2.isDone() = " + future2.isDone());
System.out.println(future2.get());
System.out.println("手动阻塞后的非业务语句3");
System.out.println("future3.isDone() = " + future3.isDone());
System.out.println(future3.get());
System.out.println("手动阻塞后的非业务语句4");
System.out.println("处理完毕" + new Date());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
@Slf4j
@Service
public class IOneServiceImpl implements IOneService {
@Async("firstExecutor")
@Override
public CompletableFuture<String> methodOne(String msg) {
log.info("first-executor:message{}", msg);
try {
Thread.sleep(15 * 1000);
} catch (InterruptedException e) {
log.error(e.getMessage());
}
// 把业务返回值改成CompletableFuture,用于手动(阻塞)控制流程
System.out.println("业务一完成" + new Date());
return CompletableFuture.completedFuture(String.format("first-executor:message{ %s }", msg));
}
@Async("firstExecutor")
@Override
public CompletableFuture<String> methodTwo(String msg) {
log.info("first-executor:message{}", msg);
try {
Thread.sleep(5 * 1000);
} catch (InterruptedException e) {
log.error(e.getMessage());
}
// 把业务返回值改成CompletableFuture,用于手动(阻塞)控制流程
System.out.println("业务二完成" + new Date());
return CompletableFuture.completedFuture(String.format("first-executor:message{ %s }", msg));
}
@Async("firstExecutor")
@Override
public CompletableFuture<String> methodThree(String msg) {
log.info("first-executor:message{}", msg);
try {
Thread.sleep(1 * 1000);
} catch (InterruptedException e) {
log.error(e.getMessage());
}
// 把业务返回值改成CompletableFuture,用于手动(阻塞)控制流程
System.out.println("业务三完成" + new Date());
return CompletableFuture.completedFuture(String.format("first-executor:message{ %s }", msg));
}
}
运行结果:
从运行结果可以看到,线程阻塞实际上是从调用.get()
方法抽取结果时开始的!!!
三、Spring中的线程池(执行器)
Spring用TaskExecutor
和TaskScheduler
接口提供了异步执行和调度任务的抽象。
Spring的TaskExecutor
和java.util.concurrent.Executor
接口时一样的,这个接口只有一个方法execute(Runnable task)
。
Spring已经内置了许多TaskExecutor
的实现,没有必要自己去实现
SimpleAsyncTaskExecutor
: 这种实现不会重用任何线程,每次调用都会创建一个新的线程。SyncTaskExecutor
: 这种实现不会异步的执行,相反,每次调用都在发起调用的线程中执行。它的主要用处是在不需要多线程的时候,比如简单的测试用例;ConcurrentTaskExecutor
:这个实现是对Java 5java.util.concurrent.Executor
类的包装。有另一个ThreadPoolTaskExecutor
类更为好用,它暴露了Executor
的配置参数作为bean属性。SimpleThreadPoolTaskExecutor
: 这个实现实际上是Quartz
的SimpleThreadPool
类的子类,它会监听Spring的生命周期回调。当你有线程池,需要在Quartz
和非Quartz
组件中共用时,这是它的典型用处。ThreadPoolTaskExecutor
: 这是最常用、最通用的一种实现。它包含了java.util.concurrent.ThreadPoolExecutor
的属性,并且用TaskExecutor
进行包装。
注意事项
@Async
注解会在以下几个场景失效,也就是说明明使用了@Async
注解,但就没有走多线程。
- 异步方法使用static关键词修饰;
- 异步类不是一个Spring容器的bean(一般使用注解
@Component
和@Service
,并且能被Spring
扫描到); SpringBoot
应用中没有添加@EnableAsync
注解;- 在同一个类中,一个方法调用另外一个有
@Async
注解的方法,注解不会生效。原因是@Async
的原理是通过Spring AOP
动态代理 的方式来实现的。
Spring容器启动初始化bean时,判断类中是否使用了@Async
注解,如果使用了则为其创建切入点和切入点处理器,根据切入点创建代理,
在线程调用@Async
注解标注的方法时,会调用代理,执行切入点处理器invoke
方法,将方法的执行提交给线程池中的另外一个线程来处理,从而实现了异步执行。
所以,需要注意的一个错误用法是,如果a方法调用它同类中的标注@Async
的b方法,是不会异步执行的,因为从a方法进入调用的都是该类对象本身,不会进入代理类。
因此,相同类中的方法调用带@Async
的方法是无法异步的,这种情况仍然是同步。 - 异步方法使用注解
@Async
的返回值只能为void
或者Future
及其子类,当返回结果为其他类型时,方法还是会异步执行,但是返回值都是null
,部分源码如下:
参考资料:
- 知乎 读钓
https://zhuanlan.zhihu.com/p/134636915
- CSDN https://blog.csdn.net/qq_44750696/article/details/123960134