線程池之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í)行過程中由于故障而終止,那么如果需要執(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í)行過程中由于故障而終止,那么如果需要執(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)造的線程(如果可用)。
如果沒有可用的現(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í)行過程中由于故障而終止,那么如果需要執(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)文章
SpringAOP實(shí)現(xiàn)日志收集管理功能(步驟詳解)
這篇文章主要介紹了SpringAOP實(shí)現(xiàn)日志收集管理功能,本文分步驟通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
Java運(yùn)行時(shí)動(dòng)態(tài)生成對(duì)象的方法小結(jié)
Java是一門靜態(tài)語(yǔ)言,通常,我們需要的class在編譯的時(shí)候就已經(jīng)生成了,為什么有時(shí)候我們還想在運(yùn)行時(shí)動(dòng)態(tài)生成class呢?今天通過本文給大家分享Java運(yùn)行時(shí)動(dòng)態(tài)生成對(duì)象的方法小結(jié),需要的朋友參考下吧2021-08-08
關(guān)于spring中不同包中類名相同報(bào)錯(cuò)問題的總結(jié)
這篇文章主要介紹了關(guān)于spring中不同包中類名相同報(bào)錯(cuò)問題的總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
java使用dbcp2數(shù)據(jù)庫(kù)連接池
這篇文章主要為大家詳細(xì)介紹了java使用dbcp2數(shù)據(jù)庫(kù)連接池的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
解決springboot中自定義JavaBean返回的json對(duì)象屬性名稱大寫變小寫問題
開發(fā)過程中發(fā)現(xiàn)查詢返回的數(shù)據(jù)出現(xiàn)自定義的JavaBean的屬性值大小寫格式出現(xiàn)問題,導(dǎo)致前端無(wú)法接受到數(shù)據(jù),目前有四種解決方法,根據(jù)大佬的經(jīng)驗(yàn)之談,前兩種是最簡(jiǎn)單便捷的,后兩種是比較通用的方法,需要的朋友可以參考下2023-10-10
Java JTable 實(shí)現(xiàn)日歷的示例
這篇文章主要介紹了Java JTable 實(shí)現(xiàn)日歷的示例,幫助大家更好的理解和學(xué)習(xí)Java jtable的使用方法,感興趣的朋友可以了解下2020-10-10

