ThreadPoolTaskScheduler和ThreadPoolTaskExecutor
繼承關(guān)系結(jié)構(gòu)圖:
藍(lán)色實(shí)現(xiàn)的箭頭是指繼承關(guān)系
綠色虛線指的是接口實(shí)現(xiàn)關(guān)系
綠色實(shí)線指的是接口繼承關(guān)系
繼承關(guān)系結(jié)構(gòu)
-
ThreadPoolTaskExecutor
image-20211209111636240.png ThreadPoolTaskScheduler

使用
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n20" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@EnableAsync
@Configuration
public class ThreadPoolConfig {
@Bean(name = AsyncExecutionAspectSupport.DEFAULT_TASK_EXECUTOR_BEAN_NAME)
public Executor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//核心線程池大小
executor.setCorePoolSize(10);
//最大線程數(shù)
executor.setMaxPoolSize(30);
//隊(duì)列容量
executor.setQueueCapacity(100);
//活躍時(shí)間
executor.setKeepAliveSeconds(60);
//線程名字前綴
executor.setThreadNamePrefix("taskExecutor-");
// 設(shè)置線程池關(guān)閉的時(shí)候等待所有任務(wù)都完成再繼續(xù)銷毀其他的Bean
executor.setWaitForTasksToCompleteOnShutdown(true);
// 線程池對(duì)拒絕任務(wù)的處理策略,當(dāng)線程池沒有處理能力的時(shí)候,該策略會(huì)直接在 execute 方法的調(diào)用線程中運(yùn)行被拒絕的任務(wù);如果執(zhí)行程序已關(guān)閉,則會(huì)丟棄該任務(wù)
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return executor;
}
@Bean("taskExecutor")
public Executor taskExecutor() {
// 創(chuàng)建一個(gè)線程池對(duì)象
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
// 定義一個(gè)線程池大小
scheduler.setPoolSize(100);
// 線程池名的前綴
scheduler.setThreadNamePrefix("taskExecutor-");
// 設(shè)置線程池關(guān)閉的時(shí)候等待所有任務(wù)都完成再繼續(xù)銷毀其他的Bean
scheduler.setWaitForTasksToCompleteOnShutdown(true);
// 設(shè)置線程池中任務(wù)的等待時(shí)間,如果超過這個(gè)時(shí)候還沒有銷毀就強(qiáng)制銷毀,以確保應(yīng)用最后能夠被關(guān)閉,而不是阻塞住
scheduler.setAwaitTerminationSeconds(60);
// 線程池對(duì)拒絕任務(wù)的處理策略,當(dāng)線程池沒有處理能力的時(shí)候,該策略會(huì)直接在 execute 方法的調(diào)用線程中運(yùn)行被拒絕的任務(wù);如果執(zhí)行程序已關(guān)閉,則會(huì)丟棄該任務(wù)
scheduler.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return scheduler;
}
}</pre>
總結(jié)
從結(jié)構(gòu)上看,兩者只相差了一個(gè)TaskScheduler,而TaskScheduler是專門用于調(diào)度任務(wù)的類,這也從根本上區(qū)分了兩者的用途
ThreadPoolTaskExecutor是一個(gè)專門用于執(zhí)行任務(wù)的類。
ThreadPoolTaskScheduler是一個(gè)專門用于調(diào)度任務(wù)的類。
從使用上看,ThreadPoolTaskExecutor可以進(jìn)行更為細(xì)粒度的劃分
因此,在兩者之間進(jìn)行選擇歸結(jié)為以下問題:我是否需要執(zhí)行或安排任務(wù)的執(zhí)行
擴(kuò)展
Reject策略預(yù)定義
RejectedExecutionHandler handler,Reject策略預(yù)定義有四種:
(1)ThreadPoolExecutor.AbortPolicy策略,是默認(rèn)的策略,處理程序遭到拒絕將拋出運(yùn)行時(shí) RejectedExecutionException。
(2)ThreadPoolExecutor.CallerRunsPolicy策略 ,調(diào)用者的線程會(huì)執(zhí)行該任務(wù),如果執(zhí)行器已關(guān)閉,則丟棄.
(3)ThreadPoolExecutor.DiscardPolicy策略,不能執(zhí)行的任務(wù)將被丟棄.
(4)ThreadPoolExecutor.DiscardOldestPolicy策略,如果執(zhí)行程序尚未關(guān)閉,則位于工作隊(duì)列頭部的任務(wù)將被刪除,然后重試執(zhí)行程序(如果再次失敗,則重復(fù)此過程)
參考:
