Springboot中異步線程池怎么玩?
點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號”
優(yōu)質(zhì)文章,第一時間送達(dá)
? 作者?|??Jack2222
來源 |? urlify.cn/IfeiEv
66套java從入門到精通實(shí)戰(zhàn)課程分享
配置線程池
首先我們需要先編寫 啟用@EnableAsync 的線程池配置類
import?java.util.concurrent.Executor;
import?java.util.concurrent.ThreadPoolExecutor;
import?org.springframework.context.annotation.Bean;
import?org.springframework.context.annotation.Configuration;
import?org.springframework.scheduling.annotation.EnableAsync;
import?org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
//啟動異步
@EnableAsync
//這是一個配置類
@Configuration
class?TaskPoolConfig?{
????//設(shè)置Bean的名稱不設(shè)置的話沒有辦法在?任務(wù)中對應(yīng)?配置信息
????@Bean("taskExecutor")
????public?Executor?taskExecutor()?{
????????//根據(jù)ThreadPoolTaskExecutor?創(chuàng)建建線程池
????????ThreadPoolTaskExecutor?executor?=?new?ThreadPoolTaskExecutor();
????????//為線程設(shè)置初始的線程數(shù)量?5條線程
????????executor.setCorePoolSize(5);
????????//為線程設(shè)置最大的線程數(shù)量?10條線程
????????executor.setMaxPoolSize(10);
????????//為任務(wù)隊(duì)列設(shè)置最大?任務(wù)數(shù)量
????????executor.setQueueCapacity(200);
????????//設(shè)置?超出初始化線程的?存在時間為60秒
????????//也就是?如果現(xiàn)有線程數(shù)超過5?則會對超出的空閑線程?設(shè)置摧毀時間?也就是60秒
????????executor.setKeepAliveSeconds(60);
????????//設(shè)置?線程前綴
????????executor.setThreadNamePrefix("taskExecutor-");
????????//線程池的飽和策略?我這里設(shè)置的是?CallerRunsPolicy?也就是由用調(diào)用者所在的線程來執(zhí)行任務(wù)?共有四種
????????//AbortPolicy:直接拋出異常,這是默認(rèn)策略;
????????//CallerRunsPolicy:用調(diào)用者所在的線程來執(zhí)行任務(wù);
????????//DiscardOldestPolicy:丟棄阻塞隊(duì)列中靠最前的任務(wù),并執(zhí)行當(dāng)前任務(wù);
????????//DiscardPolicy:直接丟棄任務(wù);
????????executor.setRejectedExecutionHandler(new?ThreadPoolExecutor.CallerRunsPolicy());
????????//設(shè)置在關(guān)閉線程池時是否等待任務(wù)完成
????????executor.setWaitForTasksToCompleteOnShutdown(true);
????????//設(shè)置等待終止的秒數(shù)
????????executor.setAwaitTerminationSeconds(60);
????????//返回設(shè)置完成的線程池
????????return?executor;
????}
}
使用線程池的類
在使用線程池的時候我們需在使用線程池的任務(wù)方法上面加上@Async注解
import?lombok.extern.slf4j.Slf4j;
import?org.springframework.scheduling.annotation.Async;
import?org.springframework.stereotype.Component;
//啟用log?在控制臺輸出信息
@Slf4j
//啟用@Component?注解將該類注入到spring容器中
@Component
public?class?test?{
????//為Hello類方法設(shè)置異步調(diào)用的配置類
????@Async("taskExecutor")
????public?void?Hello(String?hello)?throws?InterruptedException?{
????????//開始執(zhí)行任務(wù)
????????log.info("任務(wù)開始并延長執(zhí)行時間");
????????//延遲執(zhí)行
????????Thread.sleep(1000);
???????//執(zhí)行輸出
????????log.info(hello);
????}
}
測試線程池
編寫測試線程池的方法這利用的idea 的單元測試
import?org.junit.Test;
import?org.junit.runner.RunWith;
import?org.springframework.beans.factory.annotation.Autowired;
import?org.springframework.boot.test.context.SpringBootTest;
import?org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public?class?ApplicationTests?{
????//自動裝配?線程測試類
????@Autowired
????private?test?test;
????
????@Test
????public?void?test()?throws?Exception?{
????????//循環(huán)跑30個任務(wù)
????????for?(int?i?=?0;i<30;i++){
????????????test.Hello("test"+i);
????????}
????}
}
這里需要說一下Spring有自己的線程池 我的這個屬于自定義的線程池
同時Spring提供了定時任務(wù)調(diào)度的注解非常強(qiáng)大 @Scheduled+Cron配置
同時這個定時任務(wù)不與線程池發(fā)生沖突
粉絲福利:實(shí)戰(zhàn)springboot+CAS單點(diǎn)登錄系統(tǒng)視頻教程免費(fèi)領(lǐng)取
???
?長按上方微信二維碼?2 秒 即可獲取資料
感謝點(diǎn)贊支持下哈?
評論
圖片
表情
