java Executors工具類(lèi)的相關(guān)方法使用創(chuàng)建
通過(guò)Executors類(lèi)創(chuàng)建線程池
線程池的創(chuàng)建方式有很多種,可以通過(guò)Executors工具類(lèi)創(chuàng)建多種模式的線程池,Executors工具類(lèi)位于java.util.concurrent.locks包中,接下來(lái)解釋其中的方法。
首先將線程池的七個(gè)參數(shù)標(biāo)在這里: corePoolSize:表示線程池中核心線程的數(shù)量;
maximumPoolSize:表示線程池中最大線程數(shù)量;
keepAliveTime:針對(duì)救急線程的存活時(shí)間的變量,就是當(dāng)線程數(shù)量超過(guò)線程池中的核心數(shù)量時(shí),如果沒(méi)有新的任務(wù)被提交,那么核心線程外的救急線程就會(huì)保持在keepAliveTime變量值之后被銷(xiāo)毀,而不是立即銷(xiāo)毀;
unit:也是針對(duì)救急線程,表示keepAliveTime的核心單位;
workQueue:表示線程池中的阻塞隊(duì)列,阻塞隊(duì)列中存儲(chǔ)著等待執(zhí)行的任務(wù);
threadFactory:表示用來(lái)創(chuàng)建線程的線程工廠。創(chuàng)建線程池的時(shí)候,默認(rèn)的線程工廠創(chuàng)建的線程具有相同的優(yōu)先級(jí),可以為線程起一個(gè)好名字;
handler:表示線程池的拒絕策略。當(dāng)線程池中的阻塞隊(duì)列滿了的時(shí)候,線程數(shù)maximumPoolSize也達(dá)到了最大值,如果此時(shí)再有任務(wù)提交到線程池,線程池就會(huì)采取策略來(lái)拒絕任務(wù)的執(zhí)行。
??newCachedThreadPool方法
調(diào)用Executors工具類(lèi)的newCachedThreadPool方法可以創(chuàng)建一個(gè)可緩存的線程池,核心線程數(shù)為0,當(dāng)線程池中的線程數(shù)量超過(guò)了運(yùn)行任務(wù)所需要的線程數(shù),那么可以回收空閑的線程,默認(rèn)每60s回收;同時(shí)當(dāng)任務(wù)增加的時(shí)候,線程池又可以創(chuàng)建新的線程來(lái)處理任務(wù)。
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); }
??newFixThreadPool方法
調(diào)用Executors工具類(lèi)的newFixThreadPool方法可以創(chuàng)建一個(gè)固定長(zhǎng)度大小的線程池,線程池中的線程都是核心線程,也就是創(chuàng)建的線程池中的線程數(shù)量是固定的,能夠控制線程池的最大并發(fā)數(shù)。
當(dāng)向線程池提交任務(wù)時(shí),如果線程池中有空閑的線程則會(huì)直接執(zhí)行任務(wù),如果沒(méi)有空閑線程則會(huì)將任務(wù)放入阻塞隊(duì)列中等待,有空閑線程的時(shí)候再去執(zhí)行阻塞隊(duì)列中的線程。
當(dāng)線程池達(dá)到最大線程數(shù)時(shí),線程數(shù)量會(huì)保持不變,一旦某個(gè)線程出現(xiàn)異常,線程池會(huì)補(bǔ)充一個(gè)線程。提交到線程池的任務(wù)過(guò)多可能會(huì)導(dǎo)致內(nèi)存溢出。
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory); }
??newScheduledThreadPool方法
調(diào)用Executors工具類(lèi)的newScheduledThreadPool方法可以創(chuàng)建一個(gè)可以周期性執(zhí)行任務(wù)的線程池,也能夠定時(shí)執(zhí)行任務(wù)。
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) { return new ScheduledThreadPoolExecutor(corePoolSize); } public static ScheduledExecutorService newScheduledThreadPool( int corePoolSize, ThreadFactory threadFactory) { return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory); }
??newSingleThreadExecutor方法
調(diào)用Executors工具類(lèi)的newSingleThreadExecutor方法可以創(chuàng)建只有一個(gè)工作線程的線程池,相當(dāng)于只有一個(gè)線程在工作。能夠保證任務(wù)按照先進(jìn)先出的順序,或者優(yōu)先級(jí)順序執(zhí)行任務(wù)。就像單線程在串行執(zhí)行任務(wù)一樣,但是也有些區(qū)別,如果這個(gè)唯一的線程出現(xiàn)了異常,線程池會(huì)創(chuàng)建一個(gè)新的線程來(lái)代替它。
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) { return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory)); }
??newSingleThreadScheduledExecutor方法
調(diào)用Executors工具類(lèi)的newSingleThreadScheduledExecutor方法可以創(chuàng)建只有一個(gè)工作線程,并且支持定時(shí),周期性執(zhí)行任務(wù)的線程池。
public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) { return new DelegatedScheduledExecutorService (new ScheduledThreadPoolExecutor(1, threadFactory)); }
??newWorkStealingPool方法
調(diào)用Executors工具類(lèi)的newSingleThreadExecutor方法可以創(chuàng)建一個(gè)具有并行級(jí)別的線程池,這是jdk 1.8新增的方法,可以為線程池設(shè)置并行級(jí)別,通過(guò)使用多個(gè)隊(duì)列來(lái)減少競(jìng)爭(zhēng),需要傳遞一個(gè)并行級(jí)別的參數(shù)。
public static ExecutorService newWorkStealingPool(int parallelism) { return new ForkJoinPool (parallelism, ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true); }
以上就是java Executors工具類(lèi)的相關(guān)方法使用創(chuàng)建的詳細(xì)內(nèi)容,更多關(guān)于java Executors工具類(lèi)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java虛擬機(jī)參數(shù)-D、-X和-XX的區(qū)別小結(jié)
本文主要介紹了java虛擬機(jī)參數(shù)-D、-X和-XX的區(qū)別小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06Mybatis CachingExecutor二級(jí)緩存使用示例詳解
這篇文章主要介紹了?Mybatis的CachingExecutor與二級(jí)緩存使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09解決springboot啟動(dòng)成功,但訪問(wèn)404的問(wèn)題
這篇文章主要介紹了解決springboot啟動(dòng)成功,但訪問(wèn)404的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07Spring中的@PathVariable注解詳細(xì)解析
這篇文章主要介紹了Spring中的@PathVariable注解詳細(xì)解析,@PathVariable 是 Spring 框架中的一個(gè)注解,用于將 URL 中的變量綁定到方法的參數(shù)上,它通常用于處理 RESTful 風(fēng)格的請(qǐng)求,從 URL 中提取參數(shù)值,并將其傳遞給方法進(jìn)行處理,需要的朋友可以參考下2024-01-01SpringBoot中@Value獲取值和@ConfigurationProperties獲取值用法及比較
在Spring Boot中,@Value注解是一個(gè)非常有用的特性,它允許我們將外部的配置注入到我們的Bean中,@ConfigurationProperties用于將配置文件中的屬性綁定到 Java Bean 上,本文介紹了@Value獲取值和@ConfigurationProperties獲取值用法及比較,需要的朋友可以參考下2024-08-08Java 多線程synchronized關(guān)鍵字詳解(六)
這篇文章主要介紹了Java 多線程synchronized關(guān)鍵字詳解(六)的相關(guān)資料,需要的朋友可以參考下2015-12-12