Executor 框架結(jié)構(gòu)
- Executor 框架主要由 3 大部分組成如下。
- 任務(wù)芯侥。包括被執(zhí)行任務(wù)需要實(shí)現(xiàn)的接口:Runnable 接口或 Callable 接口泊交。
- 任務(wù)的執(zhí)行。包括任務(wù)執(zhí)行機(jī)制的核心接口 Executor柱查,以及繼承自Executor 的 ExecutorService 接 口廓俭。Executor 框 架 有 兩 個(gè) 關(guān) 鍵 類 實(shí)現(xiàn) 了 ExecutorService 接 口(ThreadPoolExecutor 和ScheduledThreadPoolExecutor)。
- 異步計(jì)算的結(jié)果唉工。包括接口 Future 和實(shí)現(xiàn) Future 接口的 FutureTask 類研乒。
Executor 框架的成員
-
Executor 框架的主要成員:ThreadPoolExecutor、ScheduledThreadPoolExecutor淋硝、Future 接口告嘲、Runnable 接口、Callable 接口和 Executors奖地。
-
(1)ThreadPoolExecutor:
ThreadPoolExecutor 通常使用工廠類 Executors 來(lái)創(chuàng)建橄唬。Executors 可以創(chuàng)建 3 種類型的ThreadPoolExecutor:SingleThreadExecutor、FixedThreadPool 和 CachedThreadPool参歹。
-
FixedThreadPool
:固定數(shù)量的線程池仰楚,Executors 的 newFixedThreadPool 方法提供兩個(gè)APIpublic static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); } public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory); }
FixedThreadPool 適用于為了滿足資源管理的需求,而需要限制當(dāng)前線程數(shù)量的應(yīng)用場(chǎng)景,它適用于負(fù)載比較重的服務(wù)器
-
FixedThreadPool 的 corePoolSize 和 maximumPoolSize 都被設(shè)置為創(chuàng)建是的指定參數(shù) nThreads
- 當(dāng)線程池中的線程數(shù)大于 corePoolSize 時(shí)僧界, keepAliveTime 為多余的空閑線程等待新任務(wù)的最長(zhǎng)時(shí)間侨嘀,超過(guò)時(shí)間多余的空閑線程將被終止。這里的 keepAliveTime 為0L捂襟,意味著多余的空閑線程會(huì)被立即終止
- 如果線程數(shù)少于 corePoolSize 咬腕,則創(chuàng)建新的線程來(lái)執(zhí)行任務(wù)(不管有沒(méi)有空閑的線程,都會(huì)創(chuàng)建)
- 線程池完成預(yù)熱之后(當(dāng)前運(yùn)行的線程等于corePoolSize )將任務(wù)加入 LinkedBlockingQueue
-
FixedThreadPool 使用無(wú)界隊(duì)列 LinkedBlockingQueue (隊(duì)列容量為Integer.MAX_VALUE)對(duì)線程池帶來(lái)的影響葬荷。
1)線程池線程數(shù)量到達(dá) corePoolSize 后涨共,新任務(wù)將在無(wú)界隊(duì)列中等待,因此線程池的數(shù)量不會(huì)超過(guò)corePoolSize
2)由1)宠漩,使用無(wú)界隊(duì)列時(shí) maximumPoolSize 將是無(wú)效參數(shù)
3)由于1)举反、2),使用無(wú)界隊(duì)列時(shí) keepAliveTime 將是一個(gè)無(wú)效參數(shù)扒吁。(無(wú)界隊(duì)列不會(huì)滿火鼻,將也不會(huì)出現(xiàn)已經(jīng)創(chuàng)建的線程小于最大線程數(shù),所以也不會(huì)有空閑線程 )
-
4)由于使用無(wú)界隊(duì)列雕崩,所以運(yùn)行中的 FixedThreadPool (未執(zhí)行兩個(gè) shutdown 方法)不會(huì)拒接任務(wù)(不會(huì)調(diào)用RejectedExecutionHandler.rejectedExecution 方法)
?
-
SingleThreadExecutor
:?jiǎn)蝹€(gè)線程的線程池魁索,Executors 的 newSingleThreadExecutor 方法提供了兩個(gè)APIpublic static ExecutorService newSingleThreadExecutor() { return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>())); } public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) { return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory)); }
SingleThreadExecutor 適用于需要保證順序地執(zhí)行各個(gè)任務(wù);并且在任意時(shí)間點(diǎn)盼铁,不會(huì)有多個(gè)線程是活動(dòng)的應(yīng)用場(chǎng)景
-
SingleThreadExecutor 的 corePoolSize 和 maximumPoolSize 都被設(shè)置為1蛾默,其余參數(shù)都與FixedThreadPool 相同,也是使用了無(wú)界隊(duì)列LinkedBlockingQueue作為線程池的工作隊(duì)列捉貌。帶來(lái)的影響跟 FixedThreadPool 相同
?
-
CachedThreadPool
:大小無(wú)界的線程池支鸡,Executors 的 newCachedThreadPool 方法提供了兩個(gè)APIpublic static ExecutorService newCachedThreadPool() { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); } public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), threadFactory); }
CachedThreadPool 是大小無(wú)界的線程池,適用于執(zhí)行很多短期的異步任務(wù)的小程序趁窃,或者是負(fù)載較輕的服務(wù)器
CachedThreadPool 的 corePoolSize 為0牧挣,但是 maximumPoolSize 被設(shè)置為Integer.MAX_VALUE ,所以可以說(shuō)是 無(wú)界的醒陆,這里把 keepAliveTime 設(shè)置為 60L 意味著線程池中的空閑線程最長(zhǎng)等待時(shí)間為60秒瀑构,超過(guò)將被終止
-
CachedThreadPool 使用的是沒(méi)有容量的 SynchronousQueue 隊(duì)列作為線程池的工作隊(duì)列,但maximumPoolSize 是無(wú)界的刨摩。這意味著寺晌,如果主線程提交任務(wù)的速度高于 maximumPoolSize 中的線程處理任務(wù)的速度時(shí),CachedThreadPool將會(huì)不斷的創(chuàng)建新的線程澡刹,極端的情況下會(huì)因?yàn)閯?chuàng)建過(guò)多的線程而耗盡CPU 和 內(nèi)存的資源
?
-
(2)ScheduledThreadPoolExecutor (定時(shí)任務(wù)):
-
ScheduledThreadPoolExecutor 通常使用 Executors 來(lái)創(chuàng)建呻征。 可以創(chuàng)建兩種類型的 ScheduledThreadPoolExecutor, 如下: 的 newScheduledThreadPool() 方法
- ScheduledThreadPoolExecutor 包含若干線程的 ScheduledThreadPoolExecutor
- SingleThreadScheduledExecutor 只包含一個(gè)線程的 ScheduledThreadPoolExecutor
-
創(chuàng)建固定個(gè)數(shù)線程的 ScheduledThreadPoolExecutor API:
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize){ return new ScheduledThreadPoolExecutor(corePoolSize); } public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize, ThreadFactory threadFactory) { return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory); } // -------------------- public ScheduledThreadPoolExecutor(int corePoolSize) { super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS, new DelayedWorkQueue()); } public ScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory) { super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS, new DelayedWorkQueue(), threadFactory); }
ScheduledThreadPoolExecutor 適用于需要多個(gè)后臺(tái)線程執(zhí)行周期任務(wù)罢浇,同時(shí)為了滿足資源管理的需求而需要限制后臺(tái)線程的數(shù)量的應(yīng)用場(chǎng)景陆赋。內(nèi)部實(shí)際上是用延遲隊(duì)列 DelayedWorkQueue 來(lái)實(shí)現(xiàn)
-
創(chuàng)建單個(gè)線程的 SingleThreadScheduledExecutor 的 API:
public static ScheduledExecutorService newSingleThreadScheduledExecutor() { return new DelegatedScheduledExecutorService (new ScheduledThreadPoolExecutor(1)); } public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) { return new DelegatedScheduledExecutorService (new ScheduledThreadPoolExecutor(1, threadFactory)); }
內(nèi)部實(shí)際上還是用 new ScheduledThreadPoolExecutor 的發(fā)那個(gè)是實(shí)現(xiàn)沐祷,但是包裝了一層委托 DelegatedScheduledExecutorService
適用于需要單個(gè)后臺(tái)線程執(zhí)行周期任務(wù),同時(shí)需要保證順序地執(zhí)行各個(gè)任務(wù)的應(yīng)用場(chǎng)景
-