@Async 是 Spring 3.0 提供的一個注解,用于標識某類(下的公共方法)或某方法會執行異步調用。
接下來,我們來看下 @Async 的基本使用和實現原理。
@Async 基本使用可以分為以下 3 步:
以 Spring Boot 項目為例,我們首先需要在 Spring Boot 的啟動類,也就是帶有@SpringBootApplication 注解的類上添加 @EnableAsync 注解,以開啟異步方法執行的支持,如下代碼所示:
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.scheduling.annotation.EnableAsync;@SpringBootApplication@EnableAsyncpublic class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); }}
創建異步方法是在需要異步執行的方法上添加 @Async 注解,這個方法一定是要放在被 IoC 容器管理的 Bean 中,只有被 IoC 管理的類才能實現異步調用,例如在帶有 @Service 注解的類中創建異步方法:
import org.springframework.scheduling.annotation.Async;import org.springframework.stereotype.Service;@Servicepublic class AsyncService { @Async public void performAsyncTask() { // 這里放置需要異步執行的代碼 System.out.println("異步任務正在執行,當前線程:" + Thread.currentThread().getName()); }}
在其他類或方法中,通過注入這個服務類的實例來調用異步方法。注意,直接在同一個類內部調用不會觸發異步行為,必須通過注入的實例調用,使用 new 創建的對象也不能進行異步方法調用,具體實現代碼如下:
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class MyController { @Autowired private AsyncService asyncService; @GetMapping("/startAsync") public String startAsyncTask() { asyncService.performAsyncTask(); return "異步任務已啟動"; }}
簡單來說,@Async 注解是由 AOP(面向切面)實現的,具體來說,它是由 AsyncAnnotationAdvisor 這個切面類來實現的。
在 AsyncAnnotationAdvisor 中,會使用 AsyncExecutionInterceptor 來處理 @Async 注解,它會在被 @Async 注解標識的方法被調用時,創建一個異步代理對象來執行方法。這個異步代理對象會在一個新的線程中調用被 @Async 注解標識的方法,從而實現方法的異步執行。
在 AsyncExecutionInterceptor 中,核心方法是 getDefaultExecutor 方法,使用此方法來獲取一個線程池來執行被 @Async 注解修飾的方法,它的實現源碼如下:
@Nullableprotected Executor getDefaultExecutor(@Nullable BeanFactory beanFactory) { Executor defaultExecutor = super.getDefaultExecutor(beanFactory); return (Executor)(defaultExecutor != null ? defaultExecutor : new SimpleAsyncTaskExecutor());}
此方法實現比較簡單,它是先嘗試調用父類 AsyncExecutionAspectSupport#getDefaultExecutor 方法獲取線程池,如果父類方法獲取不到線程池再用創建 SimpleAsyncTaskExecutor 對象作為 Async 的線程池返回。
而 SimpleAsyncTaskExecutor 中在執行任務時是這樣的:
protected void doExecute(Runnable task) { this.newThread(task).start();}
可以看出,在 Spring 框架中如果使用默認的 @Async 注解,它的執行比較簡單粗暴,并沒有使用線程池,而是每次創建線程來執行,所以在 Spring 框架中是不能直接使用 @Async 注解的,需要使用 @Async 注解搭配自定義的線程池,既實現 AsyncConfigurer 接口來提供自定義的 ThreadPoolTaskExecutor 來創建線程池,以確保 @Async 能真正的使用線程池來執行異步任務。
然而,在 Spring Boot 中,因為在框架啟動時,自動注入了 ThreadPoolTaskExecutor,如下源碼所示:
@ConditionalOnClass({ThreadPoolTaskExecutor.class})@AutoConfiguration@EnableConfigurationProperties({TaskExecutionProperties.class})@Import({TaskExecutorConfigurations.ThreadPoolTaskExecutorBuilderConfiguration.class, TaskExecutorConfigurations.TaskExecutorBuilderConfiguration.class, TaskExecutorConfigurations.SimpleAsyncTaskExecutorBuilderConfiguration.class, TaskExecutorConfigurations.TaskExecutorConfiguration.class})public class TaskExecutionAutoConfiguration { public static final String APPLICATION_TASK_EXECUTOR_BEAN_NAME = "applicationTaskExecutor"; public TaskExecutionAutoConfiguration() { }}
具體的構建細節源碼如下:
@Bean@ConditionalOnMissingBean({TaskExecutorBuilder.class, ThreadPoolTaskExecutorBuilder.class})ThreadPoolTaskExecutorBuilder threadPoolTaskExecutorBuilder(TaskExecutionProperties properties, ObjectProvider<ThreadPoolTaskExecutorCustomizer> threadPoolTaskExecutorCustomizers, ObjectProvider<TaskExecutorCustomizer> taskExecutorCustomizers, ObjectProvider<TaskDecorator> taskDecorator) { TaskExecutionProperties.Pool pool = properties.getPool(); ThreadPoolTaskExecutorBuilder builder = new ThreadPoolTaskExecutorBuilder(); builder = builder.queueCapacity(pool.getQueueCapacity()); builder = builder.corePoolSize(pool.getCoreSize()); builder = builder.maxPoolSize(pool.getMaxSize()); builder = builder.allowCoreThreadTimeOut(pool.isAllowCoreThreadTimeout()); builder = builder.keepAlive(pool.getKeepAlive()); TaskExecutionProperties.Shutdown shutdown = properties.getShutdown(); builder = builder.awaitTermination(shutdown.isAwaitTermination()); builder = builder.awaitTerminationPeriod(shutdown.getAwaitTerminationPeriod()); builder = builder.threadNamePrefix(properties.getThreadNamePrefix()); Stream var10001 = threadPoolTaskExecutorCustomizers.orderedStream(); Objects.requireNonNull(var10001); builder = builder.customizers(var10001::iterator); builder = builder.taskDecorator((TaskDecorator)taskDecorator.getIfUnique()); builder = builder.additionalCustomizers(taskExecutorCustomizers.orderedStream().map(this::adapt).toList()); return builder;}
因此在 Spring Boot 框架中可以直接使用 @Async 注解,無需擔心它每次都會創建線程來執行的問題。
本文鏈接:http://www.www897cc.com/showinfo-26-99169-0.html阿里面試:說說@Async實現原理?
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com
上一篇: Go語言助力安全測試:24小時內發送5億次HTTP/1.1請求
下一篇: 為什么要限流?及常用的限流算法解析