ThreadPoolExecutor
構(gòu)造方法:
/**
* Creates a new {@codeThreadPoolExecutor} with the given initial
* parameters and default thread factory and rejected execution handler.
* It may be more convenient to use one of the {@linkExecutors} factory
* methods instead of this general purpose constructor.
*
*@paramcorePoolSizethe number of threads to keep in the pool, even
*? ? ? ? if they are idle, unless {@codeallowCoreThreadTimeOut} is set
*@parammaximumPoolSizethe maximum number of threads to allow in the
*? ? ? ? pool
*@paramkeepAliveTimewhen the number of threads is greater than
*? ? ? ? the core, this is the maximum time that excess idle threads
*? ? ? ? will wait for new tasks before terminating.
*@paramunitthe time unit for the {@codekeepAliveTime} argument
*@paramworkQueuethe queue to use for holding tasks before they are
*? ? ? ? executed.? This queue will hold only the {@codeRunnable}
*? ? ? ? tasks submitted by the {@codeexecute} method.
*@throwsIllegalArgumentException if one of the following holds:
*? ? ? ? {@codecorePoolSize < 0}
*? ? ? ? {@codekeepAliveTime < 0}
*? ? ? ? {@codemaximumPoolSize <= 0}
*? ? ? ? {@codemaximumPoolSize < corePoolSize}
*@throwsNullPointerException if {@codeworkQueue} is null
*/
publicThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue workQueue) {
this(corePoolSize,maximumPoolSize,keepAliveTime,unit,workQueue,
Executors.defaultThreadFactory(),defaultHandler);
}
構(gòu)造方法參數(shù)
corePoolSize:核心線程池大小
maximumPoolSize:最大線程池大小
keepAliveTime:線程池中超過(guò)corePoolSize數(shù)目的空閑線程最大存活時(shí)間达椰;可以allowCoreThreadTimeOut(true)使得核心線程超過(guò)有效時(shí)間被kill掉
TimeUnitkeepAliveTime:時(shí)間單位
workQueue:阻塞任務(wù)隊(duì)列
threadFactory:新建線程工廠
RejectedExecutionHandler:當(dāng)提交任務(wù)數(shù)超過(guò)maxmumPoolSize+workQueue之和時(shí)婴削,任務(wù)會(huì)交給RejectedExecutionHandler來(lái)處理
其中比較容易讓人誤解的是:corePoolSize喇肋,maximumPoolSize,workQueue之間關(guān)系跨琳。
1.當(dāng)線程池小于corePoolSize時(shí),新提交任務(wù)將創(chuàng)建一個(gè)新線程執(zhí)行任務(wù),即使此時(shí)線程池中存在空閑線程夺蛇。
2.當(dāng)線程池達(dá)到corePoolSize時(shí),新提交任務(wù)將被放入workQueue中酣胀,等待線程池中任務(wù)調(diào)度執(zhí)行
3.當(dāng)workQueue已滿刁赦,且maximumPoolSize>corePoolSize時(shí),新提交任務(wù)會(huì)創(chuàng)建新線程執(zhí)行任務(wù)
4.當(dāng)提交任務(wù)數(shù)超過(guò)maximumPoolSize時(shí)闻镶,新提交任務(wù)由RejectedExecutionHandler處理
5.當(dāng)線程池中超過(guò)corePoolSize線程甚脉,空閑時(shí)間達(dá)到keepAliveTime時(shí),關(guān)閉空閑線程
6.當(dāng)設(shè)置allowCoreThreadTimeOut(true)時(shí)铆农,線程池中corePoolSize線程空閑時(shí)間達(dá)到keepAliveTime也將關(guān)閉
workQueue:
SynchronousQueue:無(wú)容量的阻塞隊(duì)列
ArrayBlockingQueue:以數(shù)組實(shí)現(xiàn)的阻塞隊(duì)列牺氨,固定大小
LinkedBlockingQueue:以鏈表實(shí)現(xiàn)的阻塞隊(duì)列,無(wú)固定大小
PriorityBlockingQueue:優(yōu)先級(jí)隊(duì)列阻塞隊(duì)列墩剖,需要傳入Comparator來(lái)比較優(yōu)先級(jí)
DelayQueue:內(nèi)部是一個(gè)PriorityQueue猴凹,優(yōu)先級(jí)高低以Delayed來(lái)決定
類繼承關(guān)系:public classDelayQueue <E extends Delayed>extends AbstractQueue implements BlockingQueue
其中Delayed
public interface Delayed extends Comparable {
long getDelay(TimeUnit unit);
}
ForwodingBlockingQueue: