摘要:創(chuàng)建一個線程池,具有固定線程數(shù),運(yùn)行在共享的無界隊(duì)列中。固定線程數(shù)源碼如下是的實(shí)現(xiàn)類。線程池中允許最大的線程數(shù)。如果線程數(shù)超過了核心線程數(shù),過量的線程在關(guān)閉前等待新任務(wù)的最大時間。處理因?yàn)榫€程邊界和隊(duì)列容量導(dǎo)致的堵塞。
1.Executors.newFixedThreadPool(int nThreads):創(chuàng)建一個線程池,具有固定線程數(shù),運(yùn)行在共享的無界隊(duì)列中。在大多數(shù)時候,線程會主動執(zhí)行任務(wù),當(dāng)所有的線程都在執(zhí)行任務(wù)時,有新的任務(wù)加入進(jìn)來,就會進(jìn)入等待隊(duì)列(可以有源源不斷的任務(wù)加入進(jìn)來,因?yàn)槭菬o界隊(duì)列),當(dāng)有空閑的線程,等待隊(duì)列中的任務(wù)就會被執(zhí)行。如果有線程在執(zhí)行過程中因?yàn)閳?zhí)行失敗要關(guān)閉,新創(chuàng)建的線程會替失敗的線程執(zhí)行接下來的任務(wù)。如果想要關(guān)閉這個線程池,可以調(diào)用ExecutorService的shutDown方法。
nThreads 固定線程數(shù)
源碼如下:
public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue()); }
2.ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue
corePoolSize 核心線程數(shù),即使是空閑的時候,線程池中也會保留線程的數(shù)量。如果設(shè)置allowCoreThreadTimeOut為true,會使用keepAliveTime作為等待工作時間,默認(rèn)為false。
maximumPoolSize 線程池中允許最大的線程數(shù)。
keepAliveTime 如果線程數(shù)超過了核心線程數(shù),過量的線程在關(guān)閉前等待新任務(wù)的最大時間。
unit keepAliveTime 參數(shù)的時間單元。
workQueue 用來存放等待執(zhí)行的任務(wù),這些任務(wù)是通過execute方法提交的Runnable任務(wù)。
源碼如下:
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueueworkQueue) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), defaultHandler); }
3.ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue
threadFactory 用來創(chuàng)建新線程的工廠。
handler 處理因?yàn)榫€程邊界和隊(duì)列容量導(dǎo)致的堵塞。
源碼如下:
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueueworkQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) { if (corePoolSize < 0 || maximumPoolSize <= 0 || maximumPoolSize < corePoolSize || keepAliveTime < 0) throw new IllegalArgumentException(); if (workQueue == null || threadFactory == null || handler == null) throw new NullPointerException(); this.acc = System.getSecurityManager() == null ? null : AccessController.getContext(); this.corePoolSize = corePoolSize; this.maximumPoolSize = maximumPoolSize; this.workQueue = workQueue; this.keepAliveTime = unit.toNanos(keepAliveTime); this.threadFactory = threadFactory; this.handler = handler; }
4.LinkedBlockingQueue:Executors.newFixedThreadPool()使用基于鏈表結(jié)點(diǎn)的無界隊(duì)列LinkedBlockingQueue存儲待執(zhí)行的任務(wù)。繼承了AbstractQueue類,實(shí)現(xiàn)了BlockingQueue接口,采用先進(jìn)先出的排列方式,頭結(jié)點(diǎn)是入隊(duì)時間最長的元素,尾結(jié)點(diǎn)是入隊(duì)時間最短的元素。新結(jié)點(diǎn)添加到隊(duì)尾,從隊(duì)頭彈出結(jié)點(diǎn)。鏈表隊(duì)列的特點(diǎn)是:跟基于數(shù)組的隊(duì)列相比有更大的吞吐量,但在大多并發(fā)應(yīng)用中性能會比較差。LinkedBlockingQueue可以在創(chuàng)建的時候傳遞一個容量參數(shù),限制隊(duì)列的長度,不設(shè)定的情況下,默認(rèn)是Integer.MAX_VALUE。在沒有超過隊(duì)列邊界的情況下,每次添加會自動創(chuàng)建鏈表結(jié)點(diǎn)。
源碼如下:
public LinkedBlockingQueue() { this(Integer.MAX_VALUE); }
public LinkedBlockingQueue(int capacity) { if (capacity <= 0) throw new IllegalArgumentException(); this.capacity = capacity; last = head = new Node(null); }
5.DefaultThreadFactory:返回默認(rèn)的線程工廠創(chuàng)建新線程。執(zhí)行器Executor在同一個線程組中創(chuàng)建所有新的線程。如果存在SecurityManager,就使用SecurityManager的線程組,否則使用當(dāng)前線程的線程組。每一個新創(chuàng)建的線程作為非守護(hù)線程,其優(yōu)先級設(shè)置為Thread.NORM_PRIORITY和線程組允許的最大優(yōu)先級的較小者??梢酝ㄟ^Thread.getName()獲取線程的名稱,形式如:pool-N-thread-M,N表示線程工廠的序列號,M表示線程工廠創(chuàng)建的線程的序列號。
源碼如下:
public static ThreadFactory defaultThreadFactory() { return new DefaultThreadFactory(); }
static class DefaultThreadFactory implements ThreadFactory { private static final AtomicInteger poolNumber = new AtomicInteger(1); private final ThreadGroup group; private final AtomicInteger threadNumber = new AtomicInteger(1); private final String namePrefix; DefaultThreadFactory() { SecurityManager s = System.getSecurityManager(); group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup(); namePrefix = "pool-" + poolNumber.getAndIncrement() + "-thread-"; } ...... }
6.AbortPolicy:默認(rèn)的拒絕執(zhí)行處理器。拋出一個RejectedExecutionException。
源碼如下:
private static final RejectedExecutionHandler defaultHandler = new AbortPolicy();
public static class AbortPolicy implements RejectedExecutionHandler { public AbortPolicy() { } public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { throw new RejectedExecutionException("Task " + r.toString() + " rejected from " + e.toString()); } }
7.例子
@Test public void testFixedDemo01() { ExecutorService executorService = Executors.newFixedThreadPool(4); for(int i = 0;i < 10;i++) { executorService.execute(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName() + " " +" 走啦"); } }); } }
運(yùn)行結(jié)果:
核心線程數(shù)和最大線程數(shù)都是4,創(chuàng)建了一個線程工廠。
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/71077.html
摘要:的前位數(shù)用來表示線程的數(shù)量,后面三位用來表示線程池的狀態(tài)。線程池的狀態(tài)有五種,分別是,根據(jù)單詞就能猜出大概。并且為了考慮性能問題,線程池的設(shè)計(jì)沒有使用悲觀鎖關(guān)鍵字,而是大量使用了和機(jī)制。 零 前期準(zhǔn)備 0 FBI WARNING 文章異常啰嗦且繞彎。 1 版本 JDK 版本 : OpenJDK 11.0.1 IDE : idea 2018.3 2 ThreadPoolExecutor ...
摘要:創(chuàng)建方法最大線程數(shù)即源碼單線程化的線程池有且僅有一個工作線程執(zhí)行任務(wù)所有任務(wù)按照指定順序執(zhí)行,即遵循隊(duì)列的入隊(duì)出隊(duì)規(guī)則創(chuàng)建方法源碼還有一個結(jié)合了和,就不介紹了,基本不用。 *本篇文章已授權(quán)微信公眾號 guolin_blog (郭霖)獨(dú)家發(fā)布 為什么用線程池 創(chuàng)建/銷毀線程伴隨著系統(tǒng)開銷,過于頻繁的創(chuàng)建/銷毀線程,會很大程度上影響處理效率 >例如: > >記創(chuàng)建線程消耗時間T1,執(zhí)行...
摘要:線程池的作用線程池能有效的處理多個線程的并發(fā)問題,避免大量的線程因?yàn)榛ハ鄰?qiáng)占系統(tǒng)資源導(dǎo)致阻塞現(xiàn)象,能夠有效的降低頻繁創(chuàng)建和銷毀線程對性能所帶來的開銷。固定的線程數(shù)由系統(tǒng)資源設(shè)置。線程池的排隊(duì)策略與有關(guān)。線程池的狀態(tài)值分別是。 線程池的作用 線程池能有效的處理多個線程的并發(fā)問題,避免大量的線程因?yàn)榛ハ鄰?qiáng)占系統(tǒng)資源導(dǎo)致阻塞現(xiàn)象,能夠有效的降低頻繁創(chuàng)建和銷毀線程對性能所帶來的開銷。 線程池的...
摘要:在前面介紹的文章中,提到了關(guān)于線程池的創(chuàng)建介紹,在文章之系列外部中第一部分有詳細(xì)的說明,請參閱文章中其實(shí)說明了外部的使用方式,但是沒有說內(nèi)部是如何實(shí)現(xiàn)的,為了加深對實(shí)現(xiàn)的理解,在使用中可以放心,我們這里將做源碼解析以及反饋到原理上,工具可 在前面介紹JUC的文章中,提到了關(guān)于線程池Execotors的創(chuàng)建介紹,在文章:《java之JUC系列-外部Tools》中第一部分有詳細(xì)的說明,請參...
摘要:去美團(tuán)面試,問到了什么是線程池,如何使用,為什么要用以下做個總結(jié)。二線程池線程池的作用線程池作用就是限制系統(tǒng)中執(zhí)行線程的數(shù)量。真正的線程池接口是。創(chuàng)建固定大小的線程池。此線程池支持定時以及周期性執(zhí)行任務(wù)的需求。 去美團(tuán)面試,問到了什么是線程池,如何使用,為什么要用,以下做個總結(jié)。關(guān)于線程之前也寫過一篇文章《高級面試題總結(jié)—線程池還能這么玩?》 1、什么是線程池:? java.util...
閱讀 1107·2023-04-25 15:42
閱讀 3764·2021-11-02 14:38
閱讀 2972·2021-09-30 09:48
閱讀 1564·2021-09-23 11:22
閱讀 3719·2021-09-06 15:02
閱讀 3316·2021-09-04 16:41
閱讀 707·2021-09-02 15:41
閱讀 2171·2021-08-26 14:13