SpringBoot 中使用 @Async
使用 @Async 注解步骤:
- 添加 @EnableAsync 注解。在主类上或者 某个类上,否则,异步方法不会生效 添加 @Async
- 注解。在异步方法上添加此注解。异步方法不能被 static 修饰需要自定义线程池,则可以配置线程池
1. 配置异步执行:在Spring Boot应用程序的主类上添加@EnableAsync注解,以启用异步执行。通常,主类是带有public static void main(String[] args)方法的类。
@EnableAsync
@SpringBootApplication
public class SpringBootDemoAsyncApplication {public static void main(String[] args) {SpringApplication.run(SpringBootDemoAsyncApplication.class, args);}
}
2. 配置一个TaskExecutor bean。Spring Boot提供了默认的SimpleAsyncTaskExecutor,但您也可以根据需要配置自定义的执行器。例如,您可以在application.properties或application.yml中添加以下配置:
spring:task:execution:pool:# 最大线程数max-size: 16# 核心线程数core-size: 16# 存活时间keep-alive: 10s# 队列大小queue-capacity: 100# 是否允许核心线程超时allow-core-thread-timeout: true# 线程名称前缀thread-name-prefix: async-task-
3. 异步方法:在您的服务类或任何其他组件中,使用@Async注解标记要异步执行的方法。
package com.xkcoding.task.Controller;import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.concurrent.*;
import java.util.function.BiConsumer;@Service
@Slf4j
public class MyAsyncService {private HashMap<String, String> stringStringHashMap = null;@Asyncpublic void executeAsyncTask() {// 模拟耗时任务try {// 业务HashMap<String, String> add = add();} catch (Exception e) {Thread.currentThread().interrupt();}System.out.println(Thread.currentThread().getName()+"executeAsyncTask异步任务完成");}
- @Async适应自定义线程池
- @Async 底层原理:就是通过线程池创建一个线程,然后去执行业务逻辑。
- @Async 注解会应用默认线程池SimpleAsyncTaskExecutor
TaskExecutor bean,以定义您的线程池配置
@Configuration
@Order(-1)
public class CustomThreadPoolConfig {@Bean("customTaskExecutor")public ThreadPoolTaskExecutor customTaskExecutor() {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();executor.setCorePoolSize(10); //核心线程数目executor.setMaxPoolSize(20); //指定最大线程数executor.setQueueCapacity(50); //队列中最大的数目executor.setThreadNamePrefix("custom-async-");; //线程名称前缀//rejection-policy:当pool已经达到max size的时候,如何处理新任务//CALLER_RUNS:不在新线程中执行任务,而是由调用者所在的线程来执行executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); //对拒绝task的处理策略executor.setKeepAliveSeconds(60); //线程空闲后的最大存活时间executor.setWaitForTasksToCompleteOnShutdown(true);executor.setAwaitTerminationSeconds(60);executor.initialize(); //加载return executor;}
}
异步调用:
public class MyAsyncService {private HashMap<String, String> stringStringHashMap = null;
// @Autowired
// ThreadPoolTaskExecutor customTaskExecutor;@Async("customTaskExecutor")public void executeAsyncTask() {//customTaskExecutor.execute(this::add);// Future<HashMap<String, String>> future = customTaskExecutor.submit(this::add);
}
execute方法分析:
1、当执行方式是execute时,可以看到堆栈异常的输出
原因:ThreadPoolExecutor.runWorker()方法中,task.run(),即执行我们的方法,如果异常的话会throw x;所以可以看到异常。
2、当执行方式是submit时,堆栈异常没有输出。但是调用Future.get()方法时,可以捕获到异常
原因:ThreadPoolExecutor.runWorker()方法中,task.run(),其实还会继续执行FutureTask.run()方法,再在此方法中c.call()调用我们的方法,
如果报错是setException(),并没有抛出异常。当我们去get()时,会将异常抛出。
3、不会影响线程池里面其他线程的正常执行
4、线程池会把这个线程移除掉,并创建一个新的线程放到线程池中
当线程异常,会调用ThreadPoolExecutor.runWorker()方法最后面的finally中的processWorkerExit(),会将此线程remove,并重新addworker()一个线程。
submit方法分析:
1、将传进来的任务封装成FutureTask,同样走execute的方法调用,然后直接返回FutureTask。
2、开始执行任务,新增或者获取一个线程去执行任务(比如刚开始是新增coreThread去执行任务)。
3、执行到task.run()时,因为是FutureTask,所以会去调用FutureTask.run()。
4、在FutureTask.run()中,c.call()执行提交的任务。如果抛出异常,并不会throw x,而是setException()保存异常。
5、当我们阻塞获取submit()方法结果时get(),才会将异常信息抛出。当然因为runWorker()没有抛出异常,所以并不会删除线程。