1. 程式人生 > 其它 >關於執行緒池的7個引數

關於執行緒池的7個引數

技術標籤:java

​官方說明如下:
/**
     * Creates a new {@code ThreadPoolExecutor} with the given initial
     * parameters.
     *
     * @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.
     * @param threadFactory the factory to use when the executor
     *        creates a new thread
     * @param handler the handler to use when execution is blocked
     *        because the thread bounds and queue capacities are reached
     * @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}
     *         or {@code threadFactory} or {@code handler} is null
     */
    public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler) {

1、corePoolSize 執行緒池的核心執行緒數

執行緒池維護的一個最小執行緒數量,即使他們是空閒狀態也不會銷燬,除非設定了allowCoreThreadTimeOut。

注意:當前執行緒數未達到corePoolSize,並且有執行緒是空閒的狀態,新任務到達時仍然會建立執行緒,直到數量等於corePoolSize。

2、maximumPoolSize 最大執行緒數

執行緒數達到corePoolSize時,會將新的任務快取到任務佇列中,任務佇列也滿的情況下,就會建立新的執行緒,直到達到maximunPoolSize。

執行緒不會無限制的建立,能建立的最大執行緒數由maximumPoolSize設定。

3、keepAliveTime存活時間

是指超過corePoolSize的這些執行緒,空閒一段時間後會被銷燬掉,keepAliveTime就是這個等待的時間。

4、unit時間單位

即keepAliveTime的時間單位。

5、workQueue 工作佇列

新任務被提交後,會先進入到此工作佇列中,任務排程時再從佇列中取出任務。可以設定不同的佇列型別及佇列的長度。

6、threadFactory執行緒工廠

用了建立新的執行緒,可以設定執行緒名等等。

7、handler 拒絕策略

當工作佇列的任務達到最大數量,並且執行緒數達到最大數量,這時新任務進來需要執行的拒絕策略。

jdk中提供了4中拒絕策略:

①CallerRunsPolicy

該策略下,在呼叫者執行緒中直接執行被拒絕任務的run方法,除非執行緒池已經shutdown,則直接拋棄任務。

②AbortPolicy

該策略下,直接丟棄任務,並丟擲RejectedExecutionException異常。

③DiscardPolicy

該策略下,直接丟棄任務,什麼都不做。

④DiscardOldestPolicy

該策略下,拋棄進入佇列最早的那個任務,然後嘗試把這次拒絕的任務放入佇列。

-----------------------------------------------------------------分割線------------------------------------------------------------------------------------

當一個任務通過execute(Runnable)方法欲新增到執行緒池時:

1)如果此時執行緒池中的數量小於corePoolSize,即使執行緒池中的執行緒都處於空閒狀態,也要建立新的執行緒來處理被新增的任務。

2)如果此時執行緒池中的數量等於 corePoolSize,但是緩衝佇列 workQueue未滿,那麼任務被放入緩衝佇列。

3)如果此時執行緒池中的數量大於corePoolSize,緩衝佇列workQueue滿,並且執行緒池中的數量小於maximumPoolSize,建新的執行緒來處理被新增的任務。

4)如果此時執行緒池中的數量大於corePoolSize,緩衝佇列workQueue滿,並且執行緒池中的數量等於maximumPoolSize,那麼通過 handler所指定的策略來處理此任務。也就是:處理任務的優先順序為:核心執行緒corePoolSize、任務佇列workQueue、最大執行緒maximumPoolSize,如果三者都滿了,使用handler處理被拒絕的任務。

當執行緒池中的執行緒數量大於 corePoolSize時,如果某執行緒空閒時間超過keepAliveTime,執行緒將被終止。這樣,執行緒池可以動態的調整池中的執行緒數。