實例講解spring boot 多線程
Spring 通過任務(wù)執(zhí)行器(TaskExecutor)來實現(xiàn)多線程和并發(fā)編程。使用ThreadPoolTaskExecutor可實現(xiàn)一個基于線程池的TaskExecutor。而實際開發(fā)中任務(wù)一般是非阻塞的,即異步的,所有我們在配置類中通過@EnableAsync開啟對異步任務(wù)的支持,并通過在實際執(zhí)行的Bean的方法中使用@Async注解來聲明其是一個異步任務(wù)。
一、配置類
package com.cenobitor.taskxecutor.config;import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;import org.springframework.context.annotation.Configuration;import org.springframework.scheduling.annotation.AsyncConfigurer;import org.springframework.scheduling.annotation.EnableAsync;import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import java.util.concurrent.Executor;@Configuration@EnableAsyncpublic class TaskExecutorConfig implements AsyncConfigurer { @Override public Executor getAsyncExecutor() { ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); taskExecutor.setCorePoolSize(5); taskExecutor.setMaxPoolSize(10); taskExecutor.setQueueCapacity(25); taskExecutor.initialize(); return taskExecutor; } @Override public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { return null; }}
1、利用@EnableAsync注解開啟異步任務(wù)支持
2、配置類實現(xiàn)AsyncConfigurer接口并重寫getAsyncExecutor方法,并返回一個ThreadPoolTaskExecutor,這樣我們就獲得了一個基于線程池TaskExecutor。
二、任務(wù)執(zhí)行類
package com.cenobitor.taskxecutor.taskservice;import org.springframework.scheduling.annotation.Async;import org.springframework.stereotype.Service;@Servicepublic class AsyncTaskService { @Async public void excuteAsyncTask(Integer i){ System.out.println('異步執(zhí)行任務(wù):'+i); } @Async public void excuteAsyncTaskPlus(Integer i){ System.out.println('異步執(zhí)行任務(wù)+1:'+(i+1)); }}
通過@Async注解表明該方法是異步方法,如果注解在類級別,則表明該類所有的方法都是異步方法,而這里的方法自動被注入使用ThreadPoolTaskExecutor作為TaskExecutor。
如果在異步方法所在類中調(diào)用異步方法,將會失效;
三、運(yùn)行
package com.cenobitor.taskxecutor;import com.cenobitor.taskxecutor.taskservice.AsyncTaskService;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskxecutorApplication.class); AsyncTaskService asyncTaskService = context.getBean(AsyncTaskService.class); for (int i = 0; i < 10; i++) { asyncTaskService.excuteAsyncTask(i); asyncTaskService.excuteAsyncTaskPlus(i); } context.close(); }}
運(yùn)行結(jié)果:
異步執(zhí)行任務(wù):0異步執(zhí)行任務(wù)+1:1異步執(zhí)行任務(wù):1異步執(zhí)行任務(wù)+1:2異步執(zhí)行任務(wù):2異步執(zhí)行任務(wù):3異步執(zhí)行任務(wù):5異步執(zhí)行任務(wù)+1:6異步執(zhí)行任務(wù):6異步執(zhí)行任務(wù)+1:7異步執(zhí)行任務(wù):7異步執(zhí)行任務(wù)+1:8異步執(zhí)行任務(wù):8異步執(zhí)行任務(wù)+1:9異步執(zhí)行任務(wù):9異步執(zhí)行任務(wù)+1:10異步執(zhí)行任務(wù)+1:3異步執(zhí)行任務(wù):4異步執(zhí)行任務(wù)+1:5異步執(zhí)行任務(wù)+1:4
注:摘抄自《JavaEE開發(fā)的顛覆者SpringBoot 實戰(zhàn)》。
以上就是實例講解spring boot 多線程的詳細(xì)內(nèi)容,更多關(guān)于spring boot 多線程的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. WMLScript的語法基礎(chǔ)2. html小技巧之td,div標(biāo)簽里內(nèi)容不換行3. XML入門的常見問題(四)4. xml中的空格之完全解說5. ASP動態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗分享6. msxml3.dll 錯誤 800c0019 系統(tǒng)錯誤:-2146697191解決方法7. 無線標(biāo)記語言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁8. 解決ASP中http狀態(tài)跳轉(zhuǎn)返回錯誤頁的問題9. ASP中if語句、select 、while循環(huán)的使用方法10. ASP中解決“對象關(guān)閉時,不允許操作?!钡脑幃悊栴}……
