Java中的ThreadPoolExecutor線程池詳解
概述
Executor 框架最核心的類是 ThreadPoolExecutor,它是線程池的實現(xiàn)類,主要由下列 4 個組件構(gòu)成。
- corePool:核心線程池的大小。
- maximumPool:最大線程池的大小。
- BlockingQueue:用來暫時保存任務的工作隊列。
- RejectedExecutionHandler:當 ThreadPoolExecutor 已經(jīng)關(guān)閉或 ThreadPoolExecutor 已經(jīng)飽和時(達到了最大線程池大小且工作隊列已滿),execute()方法將要調(diào)用的 Handler。
通過 Executor 框架的工具類 Executors,可以創(chuàng)建 3 種類型的 ThreadPoolExecutor。
- FixedThreadPool
- SingleThreadExecutor
- CachedThreadPool
FixedThreadPool 詳解
FixedThreadPool 被稱為可重用固定線程數(shù)的線程池。
public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); }
FixedThreadPool 的 corePoolSize 和 maximumPoolSize 都被設置為創(chuàng)建 FixedThreadPool 時指定的參數(shù) nThreads。
當線程池中的線程數(shù)大于 corePoolSize 時,keepAliveTime 為多余的空閑線程等待新任務的最長時間,超過這個時間后多余的線程將被終止。
這里把 keepAliveTime 設置為 0L,意味著多余的空閑線程會被立即終止。
1) 如果當前運行的線程數(shù)少于 corePoolSize,則創(chuàng)建新線程來執(zhí)行任務。
2) 在線程池完成預熱之后(當前運行的線程數(shù)等于 corePoolSize),將任務加入 LinkedBlockingQueue。
3) 線程執(zhí)行完 1 中的任務后,會在循環(huán)中反復從 LinkedBlockingQueue 獲取任務來 執(zhí)行。
4) FixedThreadPool 使用無界隊列 LinkedBlockingQueue 作為線程池的工作隊列(隊列的容量為 Integer.MAX_VALUE)。
使用無界隊列作為工作隊列會對線程池帶來如下影響。
- 當線程池中的線程數(shù)達到 corePoolSize 后,新任務將在無界隊列 中等待,因此線程池中的線程數(shù)不會超過 corePoolSize。
- 由于 1,使用無界隊列時 maximumPoolSize 將是一個無效參數(shù)。
- 由于 1 和 2,使用無界隊列時 keepAliveTime 將是一個無效參數(shù)。
- 由于使用無界隊列,運行中的 FixedThreadPool(未執(zhí)行方法 shutdown()或 shutdownNow())不會拒絕任務(不會調(diào)用 RejectedExecutionHandler.rejectedExecution 方法)。
SingleThreadExecutor 詳解
SingleThreadExecutor 是使用單個 worker 線程的 Executor。
public static ExecutorService newSingleThreadExecutor() { return new FinalizableDelegatedExecutorService( new ThreadPoolExecutor( 1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>() ) ); }
SingleThreadExecutor 的 corePoolSize 和 maximumPoolSize 被設置為1。
其他參數(shù)與 FixedThreadPool 相同。
SingleThreadExecutor 使用無界隊列 LinkedBlockingQueue 作為線程池的工作隊列(隊列的容量為 Integer.MAX_VALUE)。
SingleThreadExecutor 使用無界隊列作為工作隊列對線程池帶來的影響與 FixedThreadPool 相同。
1) 如果當前運行的線程數(shù)少于 corePoolSize(即線程池中無運行的線程),則創(chuàng)建一個新線程來執(zhí)行任務。
2) 在線程池完成預熱之后(當前線程池中有一個運行的線程),將任務加入 LinkedBlockingQueue。
3) 線程執(zhí)行完 1 中的任務后,會在一個無限循環(huán)中反復從 LinkedBlockingQueue 獲取任務來執(zhí)行。
CachedThreadPool 詳解
CachedThreadPool 是一個會根據(jù)需要創(chuàng)建新線程的線程池。
public static ExecutorService newCachedThreadPool() { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); }
CachedThreadPool 的 corePoolSize 被設置為 0,即 corePool 為空;maximumPoolSize 被設置為 Integer.MAX_VALUE,即 maximumPool 是無界的。這里把 keepAliveTime 設置為 60L,意味著 CachedThreadPool 中的空閑線程等待新任務的最長時間為 60 秒,空閑線程超過 60 秒后將會被終止。
FixedThreadPool 和 SingleThreadExecutor 使用無界隊列 LinkedBlockingQueue 作為線程池的工作隊列。
CachedThreadPool 使用沒有容量的 SynchronousQueue 作為線程池的工作隊列,但 CachedThreadPool 的 maximumPool 是無界的。
這意味著,如果主線程提交任務的速度高于 maximumPool 中線程處理任務的速度時,CachedThreadPool 會不斷創(chuàng)建新線程。極端情況下, CachedThreadPool 會因為創(chuàng)建過多線程而耗盡 CPU 和內(nèi)存資源。
1) 首先執(zhí)行 SynchronousQueue.offer(Runnable task)。如果當前 maximumPool 中有空閑線程正在執(zhí)行 SynchronousQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS),那么主線程執(zhí)行 offer 操作與空閑線程執(zhí)行的 poll 操作配對成功,主線程把任務交給空閑線程執(zhí)行,execute()方法執(zhí)行完成;否則執(zhí)行下面的步驟 2)。
2) 當初始 maximumPool 為空,或者 maximumPool 中當前沒有空閑線程時,將沒有線程執(zhí)行 SynchronousQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS)。這種情況下,步驟 1)將失敗。此時 CachedThreadPool 會創(chuàng)建一個新線程執(zhí)行任務,execute()方法執(zhí)行完成。
3) 在步驟 2)中新創(chuàng)建的線程將任務執(zhí)行完后,會執(zhí)行 SynchronousQueue.poll (keepAliveTime,TimeUnit.NANOSECONDS)。這個 poll 操作會讓空閑線程最多在 SynchronousQueue 中等待 60 秒鐘。如果 60 秒鐘內(nèi)主線程提交了一個新任務(主線程執(zhí)行步驟 1)),那么這個空閑線程將執(zhí)行主線程提交的新任務;否則,這個空閑線程將終止。由于空閑 60 秒的空閑線程會被終止,因此長時間保持空閑的 CachedThreadPool 不會使用任何資源。
SynchronousQueue 是一個沒有容量的阻塞隊列。每個插入操作必須等 待另一個線程的對應移除操作,反之亦然。CachedThreadPool 使用 SynchronousQueue, 把主線程提交的任務傳遞給空閑線程執(zhí)行。
CachedThreadPool 的任務傳遞示意圖
到此這篇關(guān)于Java中的ThreadPoolExecutor線程池詳解的文章就介紹到這了,更多相關(guān)ThreadPoolExecutor線程池內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決Springboot整合shiro時靜態(tài)資源被攔截的問題
這篇文章主要介紹了解決Springboot整合shiro時靜態(tài)資源被攔截的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01java拷貝指定目錄下所有內(nèi)容到minIO代碼實例
這篇文章主要介紹了java拷貝指定目錄下所有內(nèi)容到minIO代碼實例,創(chuàng)建桶 直接使用工具類先判斷,再創(chuàng)建即可,創(chuàng)建文件夾,需要注意以"/"結(jié)尾,實際也是在minIO上創(chuàng)建文件,只是作為目錄的表現(xiàn)形式展示,需要的朋友可以參考下2024-01-01IntelliJ IDEA下SpringBoot如何指定某一個配置文件啟動項目
這篇文章主要介紹了IntelliJ IDEA下SpringBoot如何指定某一個配置文件啟動項目問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09