使用SpringBoot構建應用時,如何使用線程處理異步任務?其實springBoot已經提供了默認的實現,通過在啟動類上加上注解@EnableAsync, 然后在需要異步處理的方法上增加注解@Async即可啟動一個線程進行異步處理。其實質類似於:new Thread(()-{System.out.print("處理異步任務")}).start()。但需要注意的是@Async
1.2 線程池配置屬性類MyThreadPoolConfig .java
/** * 線程池配置屬性類 */ @ConfigurationProperties(prefix = "mytask.pool") public class MyThreadPoolConfig { private int corePoolSize; private int maxPoolSize; private int keepAliveSeconds; private int queueCapacity; }
@EnableAsync @EnableConfigurationProperties({MyThreadPoolConfig.class}) @SpringCloudApplication public class application { public static void main(String[] args) { SpringApplication.run(application.class, args); } }
/** * 創建線程池 */ @Configuration public class MyTaskExecutePool { @Autowired private MyThreadPoolConfig config; @Bean("myTaskPool") public Executor taskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); //核心線程池大小 executor.setCorePoolSize(config.getCorePoolSize()); //最大線程數 executor.setMaxPoolSize(config.getMaxPoolSize()); //隊列容量 executor.setQueueCapacity(config.getQueueCapacity()); //活躍時間 executor.setKeepAliveSeconds(config.getKeepAliveSeconds()); //線程名字前綴 executor.setThreadNamePrefix("TaskExecutePool-"); // setRejectedExecutionHandler:當pool已經達到maxSize的時候,如何處理新進任務 // CallerRunsPolicy:不在新線程中執行任務,而是由調用者所在的線程來執行 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 等待所有任務結束后再關閉線程池 executor.setWaitForTasksToCompleteOnShutdown(true); executor.initialize(); return executor; } }
需要注意的是這樣定義的線程池在使用的時候要在@Async主鍵里指定名稱,如:@Async("mytaskExecutor"), 否則會使用spingtBoot提供的默認線程池。
/** * 重新SpringBoot默認的線程池 */ @Configuration public class OverrideAsyncTaskExecutePool implements AsyncConfigurer{ //注入配置類 @Autowired MyThreadPoolConfig config; @Override public Executor getAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); //核心線程池大小 executor.setCorePoolSize(config.getCorePoolSize()); //最大線程數 executor.setMaxPoolSize(config.getMaxPoolSize()); //隊列容量 executor.setQueueCapacity(config.getQueueCapacity()); //活躍時間 executor.setKeepAliveSeconds(config.getKeepAliveSeconds()); //線程名字前綴 executor.setThreadNamePrefix("MyExecutor-"); // setRejectedExecutionHandler:當pool已經達到max size的時候,如何處理新任務 // CallerRunsPolicy:不在新線程中執行任務,而是由調用者所在的線程來執行 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); executor.initialize(); return executor; }