溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

ThreadPoolExecutor線程池的示例分析

發(fā)布時(shí)間:2021-11-17 12:00:42 來源:億速云 閱讀:154 作者:小新 欄目:大數(shù)據(jù)

小編給大家分享一下ThreadPoolExecutor線程池的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

線程池參數(shù)

ThreadPoolExecutor內(nèi)部有四個(gè)構(gòu)造方法,下面只列出參數(shù)最多的一個(gè)(另外三個(gè)都是補(bǔ)充默認(rèn)參數(shù)后調(diào)用的這個(gè)):

/**
 * 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) {
    if (corePoolSize < 0 ||
        maximumPoolSize <= 0 ||
        maximumPoolSize < corePoolSize ||
        keepAliveTime < 0)
        throw new IllegalArgumentException();
    if (workQueue == null || threadFactory == null || handler == null)
        throw new NullPointerException();
    this.corePoolSize = corePoolSize;
    this.maximumPoolSize = maximumPoolSize;
    this.workQueue = workQueue;
    this.keepAliveTime = unit.toNanos(keepAliveTime);
    this.threadFactory = threadFactory;
    this.handler = handler;
}

其實(shí)從構(gòu)造方法的注釋中,我們就能知道每個(gè)參數(shù)的意思:

  • corePoolSize:保留在線程池中的線程數(shù)量,即使它們是閑置的,除非allowCoreThreadTimeOut被設(shè)置。

  • maximumPoolSize:線程池允許的最大線程數(shù)。

  • keepAliveTime:當(dāng)線程數(shù)大于corePoolSize時(shí),那些額外的空閑線程在停止前等待新任務(wù)的最大時(shí)間。

  • unit:keepAliveTime參數(shù)的時(shí)間單位。

  • workQueue:在任務(wù)執(zhí)行之前,用于保存任務(wù)的隊(duì)列,隊(duì)列只會(huì)保存那些使用execute方法提交的可執(zhí)行的任務(wù)。

  • threadFactory:當(dāng)線程池創(chuàng)建一個(gè)新線程時(shí)所用到的工廠。

  • handler:當(dāng)任務(wù)的執(zhí)行因?yàn)榫€程數(shù)量和隊(duì)列容量到達(dá)邊界而被阻止時(shí),所調(diào)用的handler。

線程池工作原理

當(dāng)一個(gè)新任務(wù)被提交時(shí):

  1. 當(dāng)前活躍線程數(shù)<corePoolSize,則創(chuàng)建一個(gè)新線程執(zhí)行新任務(wù);

  2. 當(dāng)前活躍線程數(shù)>corePoolSize,且隊(duì)列(workQueue)未滿時(shí),則將新任務(wù)放入隊(duì)列中;

  3. 當(dāng)前活躍線程數(shù)>corePoolSize,且隊(duì)列(workQueue)已滿,且當(dāng)前活躍線程數(shù)<maximumPoolSize,則繼續(xù)創(chuàng)建一個(gè)新線程執(zhí)行新任務(wù);

  4. 當(dāng)前活躍線程數(shù)>corePoolSize,且隊(duì)列(workQueue)已滿,且當(dāng)前活躍線程數(shù)=maximumPoolSize,則執(zhí)行拒絕策略(handler);

當(dāng)任務(wù)執(zhí)行完成后:

  1. 超出corePoolSize的空閑線程,在等待新任務(wù)時(shí),如果超出了keepAliveTime,則線程會(huì)被銷毀;

  2. 如果allowCoreThreadTimeOut被設(shè)置為true,那么corePoolSize以內(nèi)的空閑線程,如果超出了keepAliveTime,則同樣會(huì)被銷毀。

線程池隊(duì)列

通過構(gòu)造方法可以知道,workQueue參數(shù)是BlockingQueue<Runnable>類型的,而BlockingQueue是JUC包里的一個(gè)接口,其實(shí)現(xiàn)有:

ThreadPoolExecutor線程池的示例分析

其中,常用的隊(duì)列如下:

  1. ArrayBlockingQueue:規(guī)定大小的BlockingQueue,其構(gòu)造必須指定大小。其所含的對象是FIFO順序排序的。

  2. LinkedBlockingQueue:大小不固定的BlockingQueue,若其構(gòu)造時(shí)指定大小,生成的BlockingQueue有大小限制,不指定大小,其大小有Integer.MAX_VALUE來決定。其所含的對象是FIFO順序排序的。

  3. PriorityBlockingQueue:類似于LinkedBlockingQueue,但是其所含對象的排序不是FIFO,而是依據(jù)對象的自然順序或者構(gòu)造函數(shù)的Comparator決定。

  4. SynchronizedQueue:特殊的BlockingQueue,對其的操作必須是放和取交替完成。

線程池拒絕策略

同樣,通過構(gòu)造方法可以知道,handler參數(shù)是RejectedExecutionHandler類型的,而RejectedExecutionHandler同樣是JUC包里的一個(gè)接口,其實(shí)現(xiàn)有:

ThreadPoolExecutor線程池的示例分析

  1. DiscardOldestPolicy:丟掉緩存在隊(duì)列中的最早的任務(wù),然后重新嘗試運(yùn)行新任務(wù)。

  2. AbortPolicy:直接拒絕添加新任務(wù),并拋出異常。

  3. CallerRunsPolicy:使用當(dāng)前線程執(zhí)行該任務(wù),而不是使用線程池中的線程。

  4. DiscardPolicy:刪除提交的新任務(wù) 。

以上是“ThreadPoolExecutor線程池的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI