線程池之jdk1.8 Executors創(chuàng)建線程池的幾種方式
1、newFixedThreadPool - 定長(zhǎng)線程池
創(chuàng)建一個(gè)線程池,該線程池重用在共享無(wú)界隊(duì)列上運(yùn)行的固定數(shù)量的線程。
在任何時(shí)候,線程最多都是活動(dòng)的處理任務(wù)。如果在所有線程都處于活動(dòng)狀態(tài)時(shí)提交其他任務(wù),它們將在隊(duì)列中等待,直到有線程可用。
如果任何線程在關(guān)機(jī)前的執(zhí)行過(guò)程中由于故障而終止,那么如果需要執(zhí)行后續(xù)任務(wù),將有一個(gè)新線程替代它。
池中的線程將一直存在,直到顯式關(guān)閉。
public 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); }
2、newSingleThreadExecutor - 單一線程池
創(chuàng)建一個(gè)執(zhí)行器,該執(zhí)行器使用一個(gè)工作線程在無(wú)界隊(duì)列上運(yùn)行。
(但是請(qǐng)注意,如果此單線程在關(guān)機(jī)前的執(zhí)行過(guò)程中由于故障而終止,那么如果需要執(zhí)行后續(xù)任務(wù),將使用一個(gè)新線程代替它。)
任務(wù)保證按順序執(zhí)行,并且在任何給定時(shí)間都不會(huì)有多個(gè)任務(wù)處于活動(dòng)狀態(tài)。
與其他等效的newFixedThreadPool(1)不同,返回的執(zhí)行器保證不可重新配置以使用其他線程。
public 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)); }
newSingleThreadExecutor和newFixedThreadPool(1)區(qū)別
3、newCachedThreadPool - 緩存線程池
創(chuàng)建一個(gè)線程池,該線程池根據(jù)需要?jiǎng)?chuàng)建新線程,但在以前構(gòu)造的線程可用時(shí)將重用這些線程。
這些池通常會(huì)提高執(zhí)行許多短期異步任務(wù)的程序的性能。
調(diào)用execute將重用以前構(gòu)造的線程(如果可用)。
如果沒(méi)有可用的現(xiàn)有線程,將創(chuàng)建一個(gè)新線程并將其添加到池中。
60秒未使用的線程將被終止并從緩存中刪除。
因此,閑置足夠長(zhǎng)時(shí)間的池不會(huì)消耗任何資源。
請(qǐng)注意,可以使用ThreadPoolExecutor構(gòu)造函數(shù)創(chuàng)建具有類似屬性但不同細(xì)節(jié)(例如超時(shí)參數(shù))的池。
public 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); }
4、newScheduledThreadPool - 調(diào)度線程池
創(chuàng)建一個(gè)線程池,該線程池可以安排命令在給定延遲后運(yùn)行,或定期執(zhí)行。
參數(shù):
corePoolSize – 池中要保留的線程數(shù),即使它們處于空閑狀態(tài)
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) { return new ScheduledThreadPoolExecutor(corePoolSize); }
public static ScheduledExecutorService newScheduledThreadPool( int corePoolSize, ThreadFactory threadFactory) { return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory); }
5、newSingleThreadScheduledExecutor - 單線程調(diào)度線程池
創(chuàng)建一個(gè)單線程執(zhí)行器,該執(zhí)行器可以安排命令在給定延遲后運(yùn)行,或定期執(zhí)行。
(但是請(qǐng)注意,如果此單線程在關(guān)機(jī)前的執(zhí)行過(guò)程中由于故障而終止,那么如果需要執(zhí)行后續(xù)任務(wù),將使用一個(gè)新線程代替它。)
任務(wù)保證按順序執(zhí)行,并且在任何給定時(shí)間都不會(huì)有多個(gè)任務(wù)處于活動(dòng)狀態(tài)。
與其他等效的newScheduledThreadPool(1)不同,返回的執(zhí)行器保證不可重新配置以使用其他線程。
public static ScheduledExecutorService newSingleThreadScheduledExecutor() { return new DelegatedScheduledExecutorService (new ScheduledThreadPoolExecutor(1)); }
public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) { return new DelegatedScheduledExecutorService (new ScheduledThreadPoolExecutor(1, threadFactory)); }
6、newWorkStealingPool - 搶占操作線程池
創(chuàng)建一個(gè)線程池,該線程池維護(hù)足夠多的線程以支持給定的并行度級(jí)別,并且可以使用多個(gè)隊(duì)列來(lái)減少爭(zhēng)用。
并行級(jí)別對(duì)應(yīng)于積極參與或可參與任務(wù)處理的最大線程數(shù)。
線程的實(shí)際數(shù)量可能會(huì)動(dòng)態(tài)增長(zhǎng)和收縮。
工作竊取池不保證提交任務(wù)的執(zhí)行順序。
參數(shù):
并行度——目標(biāo)并行度級(jí)別
public static ExecutorService newWorkStealingPool(int parallelism) { return new ForkJoinPool (parallelism, ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true); }
public static ExecutorService newWorkStealingPool() { return new ForkJoinPool (Runtime.getRuntime().availableProcessors(), ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true); }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)精準(zhǔn)Excel數(shù)據(jù)排序的方法詳解
在數(shù)據(jù)處理或者數(shù)據(jù)分析的場(chǎng)景中,需要對(duì)已有的數(shù)據(jù)進(jìn)行排序,在Excel中可以通過(guò)排序功能進(jìn)行整理數(shù)據(jù),而在Java中,則可以借助Excel表格插件對(duì)數(shù)據(jù)進(jìn)行批量排序,下面我們就來(lái)學(xué)習(xí)一下常見(jiàn)的數(shù)據(jù)排序方法吧2023-10-10SpringBatch從入門到精通之StepScope作用域和用法詳解
這篇文章主要介紹了SpringBatch從入門到精通之StepScope作用域和用法詳解,主要包括IOC容器中幾種bean的作用范圍以及可能遇到的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05IDEA配置maven環(huán)境的詳細(xì)教程(Unable to import maven project報(bào)錯(cuò)問(wèn)題的解決)
這篇文章主要介紹了IDEA配置maven環(huán)境的詳細(xì)教程(Unable to import maven project問(wèn)題的解決),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06哲學(xué)家就餐問(wèn)題中的JAVA多線程學(xué)習(xí)
哲學(xué)家就餐問(wèn)題是1965年由Dijkstra提出的一種線程同步的問(wèn)題,下面我們就看一下JAVA多線程如何做2013-11-11Spring?Cloud?通過(guò)?Gateway?webflux實(shí)現(xiàn)網(wǎng)關(guān)異常處理
在某一個(gè)服務(wù)中出現(xiàn)異常,通過(guò)@ControllerAdvice?+?@ExceptionHandler?統(tǒng)一異常處理,即使在微服務(wù)架構(gòu)中,也可以將上述統(tǒng)一異常處理放入到公共的微服務(wù)中,這樣哪一個(gè)微服務(wù)需要,直接引入模塊,本文重點(diǎn)介紹Spring?Cloud?通過(guò)?Gateway?webflux實(shí)現(xiàn)網(wǎng)關(guān)異常處理,一起看看吧2023-11-11手把手帶你實(shí)現(xiàn)一個(gè)萌芽版的Spring容器
大家好,我是老三,Spring是我們最常用的開(kāi)源框架,經(jīng)過(guò)多年發(fā)展,Spring已經(jīng)發(fā)展成枝繁葉茂的大樹(shù),讓我們難以窺其全貌,這節(jié),我們回歸Spring的本質(zhì),五分鐘手?jǐn)]一個(gè)Spring容器,揭開(kāi)Spring神秘的面紗2022-03-03