您好,登錄后才能下訂單哦!
本篇內容主要講解“ThreadPoolExecutor參數如何使用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“ThreadPoolExecutor參數如何使用”吧!
1、corePoolSize:核心線程數
* 核心線程會一直存活,及時沒有任務需要執行
* 當線程數小于核心線程數時,即使有線程空閑,線程池也會優先創建新線程處理
* 設置allowCoreThreadTimeout=true(默認false)時,核心線程會超時關閉
2、queueCapacity:任務隊列容量(阻塞隊列)
* 當核心線程數達到最大時,新任務會放在隊列中排隊等待執行
3、maxPoolSize:最大線程數
* 當線程數>=corePoolSize,且任務隊列已滿時。線程池會創建新線程來處理任務
* 當線程數=maxPoolSize,且任務隊列已滿時,線程池會拒絕處理任務而拋出異常
4、 keepAliveTime:線程空閑時間
* 當線程空閑時間達到keepAliveTime時,線程會退出,直到線程數量=corePoolSize
* 如果allowCoreThreadTimeout=true,則會直到線程數量=0
5、allowCoreThreadTimeout:允許核心線程超時
6、rejectedExecutionHandler:任務拒絕處理器
* 兩種情況會拒絕處理任務:
- 當線程數已經達到maxPoolSize,切隊列已滿,會拒絕新任務
- 當線程池被調用shutdown()后,會等待線程池里的任務執行完畢,再shutdown。如果在調用shutdown()和線程池真正shutdown之間提交任務,會拒絕新任務
* 線程池會調用rejectedExecutionHandler來處理這個任務。如果沒有設置默認是AbortPolicy,會拋出異常
* ThreadPoolExecutor類有幾個內部實現類來處理這類情況:
- AbortPolicy
丟棄任務,拋運行時異常
- CallerRunsPolicy
執行任務
- DiscardPolicy
忽視,什么都不會發生
- DiscardOldestPolicy
從隊列中踢出最先進入隊列(最后一個執行)的任務
* 實現RejectedExecutionHandler接口,可自定義處理器
線程池按以下行為執行任務
1. 當線程數小于核心線程數時,創建線程。
2. 當線程數大于等于核心線程數,且任務隊列未滿時,將任務放入任務隊列。
3. 當線程數大于等于核心線程數,且任務隊列已滿
-1 若線程數小于最大線程數,創建線程
-2 若線程數等于最大線程數,拋出異常,拒絕任務
1、默認值
* corePoolSize=1 * queueCapacity=Integer.MAX_VALUE * maxPoolSize=Integer.MAX_VALUE * keepAliveTime=60s * allowCoreThreadTimeout=false * rejectedExecutionHandler=AbortPolicy()
2、如何來設置
* 需要根據幾個值來決定
- tasks
:每秒的任務數,假設為1000
- taskcost
:每個任務花費時間,假設為0.1s
- responsetime
:系統允許容忍的最大響應時間,假設為1s
* 做幾個計算
- corePoolSize = 每秒需要多少個線程處理?
* 一顆CPU核心同一時刻只能執行一個線程,然后操作系統切換上下文,核心開始執行另一個線程的代碼,以此類推,超過cpu核心數,就會放入隊列,如果隊列也滿了,就另起一個新的線程執行,所有推薦:corePoolSize = ((cpu核心數 * 2) + 有效磁盤數),java可以使用Runtime.getRuntime().availableProcessors()獲取cpu核心數
- queueCapacity = (coreSizePool/taskcost)*responsetime
* 計算可得 queueCapacity = corePoolSize/0.1*1。意思是隊列里的線程可以等待1s,超過了的需要新開線程來執行
* 切記不能設置為Integer.MAX_VALUE,這樣隊列會很大,線程數只會保持在corePoolSize大小,當任務陡增時,不能新開線程來執行,響應時間會隨之陡增。
- maxPoolSize = (max(tasks)- queueCapacity)/(1/taskcost)
* 計算可得 maxPoolSize = (1000-corePoolSize)/10,即(每秒并發數-corePoolSize大小) / 10
* (最大任務數-隊列容量)/每個線程每秒處理能力 = 最大線程數
- rejectedExecutionHandler:根據具體情況來決定,任務不重要可丟棄,任務重要則要利用一些緩沖機制來處理
- keepAliveTime和allowCoreThreadTimeout采用默認通常能滿足
ThreadPoolExecutor的執行流程有一點可能被吐槽過,就是只有緩存隊列已經滿了的時候才會使用到maxPoolSize創建新的線程.也就是說如果corePoolSize設為0的時候,要等到隊列滿了,才會創建線程去執行任務
之前有被問到,希望沒有任務的時候線程池里的線程可以停掉。可能對性能和資源有過考慮的人都會想到這個問題吧
今天看JDK源碼的時候發現了ThreadPoolExecutor在1.6的時候已經支持了
allowCoreThreadTimeOut參數就是為此設計的
/** * 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構造函數的注釋上也有明確說明:corePoolSize 的數量會一直保持,即使這些線程是空閑的,除非設置了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的靜態工廠里面的newCachedThreadPool提供了另外一種思路
coreSize為0 SynchronousQueue最多只能有一個任務在隊列里面
也就是說這個線程池的任務會被立即分配一個線程去處理,如果沒有空閑的線程會立即創建線程。
在空閑的時候,線程數量會減少直至為0,這一點倒是滿足了要求。可是隊列中最多只會緩存一個任務,當任務的處理速度慢于任務進入線程池的速度時,線程數量就會不斷膨脹。如果maxPoolSize設置成一個比較小的數字時,可能就會有大量任務被拒絕策略處理。
所以正如注釋中所說,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>());
到此,相信大家對“ThreadPoolExecutor參數如何使用”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。