ThreadPoolExecutor參數(shù)的用法及說明
ThreadPoolExecutor參數(shù)說明
一、ThreadPoolExecutor核心參數(shù)說明
1、corePoolSize:核心線程數(shù)
* 核心線程會一直存活,及時沒有任務需要執(zhí)行
* 當線程數(shù)小于核心線程數(shù)時,即使有線程空閑,線程池也會優(yōu)先創(chuàng)建新線程處理
* 設置allowCoreThreadTimeout=true(默認false)時,核心線程會超時關(guān)閉
2、queueCapacity:任務隊列容量(阻塞隊列)
* 當核心線程數(shù)達到最大時,新任務會放在隊列中排隊等待執(zhí)行
3、maxPoolSize:最大線程數(shù)
* 當線程數(shù)>=corePoolSize,且任務隊列已滿時。線程池會創(chuàng)建新線程來處理任務
* 當線程數(shù)=maxPoolSize,且任務隊列已滿時,線程池會拒絕處理任務而拋出異常
4、 keepAliveTime:線程空閑時間
* 當線程空閑時間達到keepAliveTime時,線程會退出,直到線程數(shù)量=corePoolSize
* 如果allowCoreThreadTimeout=true,則會直到線程數(shù)量=0
5、allowCoreThreadTimeout:允許核心線程超時
6、rejectedExecutionHandler:任務拒絕處理器
* 兩種情況會拒絕處理任務:
- - 當線程數(shù)已經(jīng)達到maxPoolSize,切隊列已滿,會拒絕新任務
- - 當線程池被調(diào)用shutdown()后,會等待線程池里的任務執(zhí)行完畢,再shutdown。如果在調(diào)用shutdown()和線程池真正shutdown之間提交任務,會拒絕新任務
* 線程池會調(diào)用rejectedExecutionHandler來處理這個任務。如果沒有設置默認是AbortPolicy,會拋出異常
* ThreadPoolExecutor類有幾個內(nèi)部實現(xiàn)類來處理這類情況:
- -
AbortPolicy丟棄任務,拋運行時異常 - -
CallerRunsPolicy執(zhí)行任務 - -
DiscardPolicy忽視,什么都不會發(fā)生 - -
DiscardOldestPolicy從隊列中踢出最先進入隊列(最后一個執(zhí)行)的任務
* 實現(xiàn)RejectedExecutionHandler接口,可自定義處理器
二、ThreadPoolExecutor執(zhí)行順序
線程池按以下行為執(zhí)行任務
1. 當線程數(shù)小于核心線程數(shù)時,創(chuàng)建線程。
2. 當線程數(shù)大于等于核心線程數(shù),且任務隊列未滿時,將任務放入任務隊列。
3. 當線程數(shù)大于等于核心線程數(shù),且任務隊列已滿
- -1 若線程數(shù)小于最大線程數(shù),創(chuàng)建線程
- -2 若線程數(shù)等于最大線程數(shù),拋出異常,拒絕任務
三、ThreadPoolExecutor如何設置參數(shù)
1、默認值
* corePoolSize=1 * queueCapacity=Integer.MAX_VALUE * maxPoolSize=Integer.MAX_VALUE * keepAliveTime=60s * allowCoreThreadTimeout=false * rejectedExecutionHandler=AbortPolicy()
2、如何來設置
* 需要根據(jù)幾個值來決定
- -
tasks:每秒的任務數(shù),假設為1000 - -
taskcost:每個任務花費時間,假設為0.1s - -
responsetime:系統(tǒng)允許容忍的最大響應時間,假設為1s
* 做幾個計算
- corePoolSize = 每秒需要多少個線程處理?
* 一顆CPU核心同一時刻只能執(zhí)行一個線程,然后操作系統(tǒng)切換上下文,核心開始執(zhí)行另一個線程的代碼,以此類推,超過cpu核心數(shù),就會放入隊列,如果隊列也滿了,就另起一個新的線程執(zhí)行,所有推薦:corePoolSize = ((cpu核心數(shù) * 2) + 有效磁盤數(shù)),java可以使用Runtime.getRuntime().availableProcessors()獲取cpu核心數(shù)
- queueCapacity = (coreSizePool/taskcost)*responsetime
* 計算可得 queueCapacity = corePoolSize/0.1*1。意思是隊列里的線程可以等待1s,超過了的需要新開線程來執(zhí)行
* 切記不能設置為Integer.MAX_VALUE,這樣隊列會很大,線程數(shù)只會保持在corePoolSize大小,當任務陡增時,不能新開線程來執(zhí)行,響應時間會隨之陡增。
- maxPoolSize = (max(tasks)- queueCapacity)/(1/taskcost)
* 計算可得 maxPoolSize = (1000-corePoolSize)/10,即(每秒并發(fā)數(shù)-corePoolSize大小) / 10
* (最大任務數(shù)-隊列容量)/每個線程每秒處理能力 = 最大線程數(shù)
- - rejectedExecutionHandler:根據(jù)具體情況來決定,任務不重要可丟棄,任務重要則要利用一些緩沖機制來處理
- - keepAliveTime和allowCoreThreadTimeout采用默認通常能滿足
ThreadPoolExecutor參數(shù)allowCoreThreadTimeOut
ThreadPoolExecutor的執(zhí)行流程有一點可能被吐槽過,就是只有緩存隊列已經(jīng)滿了的時候才會使用到maxPoolSize創(chuàng)建新的線程.也就是說如果corePoolSize設為0的時候,要等到隊列滿了,才會創(chuàng)建線程去執(zhí)行任務
之前有被問到,希望沒有任務的時候線程池里的線程可以停掉??赡軐π阅芎唾Y源有過考慮的人都會想到這個問題吧
今天看JDK源碼的時候發(fā)現(xiàn)了ThreadPoolExecutor在1.6的時候已經(jīng)支持了
allowCoreThreadTimeOut參數(shù)就是為此設計的
/**
* Sets the policy governing whether core threads may time out and
* terminate if no tasks arrive within the keep-alive time, being
* replaced if needed when new tasks arrive. When false, core
* threads are never terminated due to lack of incoming
* tasks. When true, the same keep-alive policy applying to
* non-core threads applies also to core threads. To avoid
* continual thread replacement, the keep-alive time must be
* greater than zero when setting {@code true}. This method
* should in general be called before the pool is actively used.
*
* @param value {@code true} if should time out, else {@code false}
* @throws IllegalArgumentException if value is {@code true}
* and the current keep-alive time is not greater than zero
*
* @since 1.6
*/
public void allowCoreThreadTimeOut(boolean value) {
if (value && keepAliveTime <= 0)
throw new IllegalArgumentException("Core threads must have nonzero keep alive times");
if (value != allowCoreThreadTimeOut) {
allowCoreThreadTimeOut = value;
if (value)
interruptIdleWorkers();
}
}
在ThreadPoolExecutor構(gòu)造函數(shù)的注釋上也有明確說明:corePoolSize 的數(shù)量會一直保持,即使這些線程是空閑的,除非設置了allowCoreThreadTimeOut
/**
* Creates a new {@code ThreadPoolExecutor} with the given initial
* parameters and default thread factory and rejected execution handler.
* It may be more convenient to use one of the {@link Executors} factory
* methods instead of this general purpose constructor.
*
* @param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @param maximumPoolSize the maximum number of threads to allow in the
* pool
* @param keepAliveTime when 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.
* @param unit the time unit for the {@code keepAliveTime} argument
* @param workQueue the queue to use for holding tasks before they are
* executed. This queue will hold only the {@code Runnable}
* tasks submitted by the {@code execute} method.
* @throws IllegalArgumentException if one of the following holds:<br>
* {@code corePoolSize < 0}<br>
* {@code keepAliveTime < 0}<br>
* {@code maximumPoolSize <= 0}<br>
* {@code maximumPoolSize < corePoolSize}
* @throws NullPointerException if {@code workQueue} is null
*/
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}
當然,在Executors的靜態(tài)工廠里面的newCachedThreadPool提供了另外一種思路
coreSize為0 SynchronousQueue最多只能有一個任務在隊列里面
也就是說這個線程池的任務會被立即分配一個線程去處理,如果沒有空閑的線程會立即創(chuàng)建線程。
在空閑的時候,線程數(shù)量會減少直至為0,這一點倒是滿足了要求??墒顷犃兄凶疃嘀粫彺嬉粋€任務,當任務的處理速度慢于任務進入線程池的速度時,線程數(shù)量就會不斷膨脹。如果maxPoolSize設置成一個比較小的數(shù)字時,可能就會有大量任務被拒絕策略處理。
所以正如注釋中所說,newCachedThreadPool只適合于任務處理速度很快的場景下。比如做一些計算,不需要依賴其它服務
/**
* Creates a thread pool that creates new threads as needed, but
* will reuse previously constructed threads when they are
* available. These pools will typically improve the performance
* of programs that execute many short-lived asynchronous tasks.
* Calls to {@code execute} will reuse previously constructed
* threads if available. If no existing thread is available, a new
* thread will be created and added to the pool. Threads that have
* not been used for sixty seconds are terminated and removed from
* the cache. Thus, a pool that remains idle for long enough will
* not consume any resources. Note that pools with similar
* properties but different details (for example, timeout parameters)
* may be created using {@link ThreadPoolExecutor} constructors.
*
* @return the newly created thread pool
*/
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring AOP手動實現(xiàn)簡單動態(tài)代理的代碼
今天小編就為大家分享一篇關(guān)于Spring AOP手動實現(xiàn)簡單動態(tài)代理的代碼,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03

