線程池之jdk1.8 Executors創(chuàng)建線程池的幾種方式
1、newFixedThreadPool - 定長線程池
創(chuàng)建一個線程池,該線程池重用在共享無界隊列上運行的固定數(shù)量的線程。
在任何時候,線程最多都是活動的處理任務(wù)。如果在所有線程都處于活動狀態(tài)時提交其他任務(wù),它們將在隊列中等待,直到有線程可用。
如果任何線程在關(guān)機前的執(zhí)行過程中由于故障而終止,那么如果需要執(zhí)行后續(xù)任務(wù),將有一個新線程替代它。
池中的線程將一直存在,直到顯式關(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)建一個執(zhí)行器,該執(zhí)行器使用一個工作線程在無界隊列上運行。
(但是請注意,如果此單線程在關(guān)機前的執(zhí)行過程中由于故障而終止,那么如果需要執(zhí)行后續(xù)任務(wù),將使用一個新線程代替它。)
任務(wù)保證按順序執(zhí)行,并且在任何給定時間都不會有多個任務(wù)處于活動狀態(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)建一個線程池,該線程池根據(jù)需要創(chuàng)建新線程,但在以前構(gòu)造的線程可用時將重用這些線程。
這些池通常會提高執(zhí)行許多短期異步任務(wù)的程序的性能。
調(diào)用execute將重用以前構(gòu)造的線程(如果可用)。
如果沒有可用的現(xiàn)有線程,將創(chuàng)建一個新線程并將其添加到池中。
60秒未使用的線程將被終止并從緩存中刪除。
因此,閑置足夠長時間的池不會消耗任何資源。
請注意,可以使用ThreadPoolExecutor構(gòu)造函數(shù)創(chuàng)建具有類似屬性但不同細(xì)節(jié)(例如超時參數(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)建一個線程池,該線程池可以安排命令在給定延遲后運行,或定期執(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)建一個單線程執(zhí)行器,該執(zhí)行器可以安排命令在給定延遲后運行,或定期執(zhí)行。
(但是請注意,如果此單線程在關(guān)機前的執(zhí)行過程中由于故障而終止,那么如果需要執(zhí)行后續(xù)任務(wù),將使用一個新線程代替它。)
任務(wù)保證按順序執(zhí)行,并且在任何給定時間都不會有多個任務(wù)處于活動狀態(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)建一個線程池,該線程池維護足夠多的線程以支持給定的并行度級別,并且可以使用多個隊列來減少爭用。
并行級別對應(yīng)于積極參與或可參與任務(wù)處理的最大線程數(shù)。
線程的實際數(shù)量可能會動態(tài)增長和收縮。
工作竊取池不保證提交任務(wù)的執(zhí)行順序。
參數(shù):
并行度——目標(biāo)并行度級別
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é)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java實現(xiàn)精準(zhǔn)Excel數(shù)據(jù)排序的方法詳解
在數(shù)據(jù)處理或者數(shù)據(jù)分析的場景中,需要對已有的數(shù)據(jù)進行排序,在Excel中可以通過排序功能進行整理數(shù)據(jù),而在Java中,則可以借助Excel表格插件對數(shù)據(jù)進行批量排序,下面我們就來學(xué)習(xí)一下常見的數(shù)據(jù)排序方法吧2023-10-10SpringBatch從入門到精通之StepScope作用域和用法詳解
這篇文章主要介紹了SpringBatch從入門到精通之StepScope作用域和用法詳解,主要包括IOC容器中幾種bean的作用范圍以及可能遇到的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05IDEA配置maven環(huán)境的詳細(xì)教程(Unable to import maven project報錯問題的解決)
這篇文章主要介紹了IDEA配置maven環(huán)境的詳細(xì)教程(Unable to import maven project問題的解決),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06哲學(xué)家就餐問題中的JAVA多線程學(xué)習(xí)
哲學(xué)家就餐問題是1965年由Dijkstra提出的一種線程同步的問題,下面我們就看一下JAVA多線程如何做2013-11-11Spring?Cloud?通過?Gateway?webflux實現(xiàn)網(wǎng)關(guān)異常處理
在某一個服務(wù)中出現(xiàn)異常,通過@ControllerAdvice?+?@ExceptionHandler?統(tǒng)一異常處理,即使在微服務(wù)架構(gòu)中,也可以將上述統(tǒng)一異常處理放入到公共的微服務(wù)中,這樣哪一個微服務(wù)需要,直接引入模塊,本文重點介紹Spring?Cloud?通過?Gateway?webflux實現(xiàn)網(wǎng)關(guān)異常處理,一起看看吧2023-11-11