最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
ThreadPoolExecutor線程池的示例分析

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

十多年的巴楚網(wǎng)站建設(shè)經(jīng)驗(yàn),針對設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。全網(wǎng)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整巴楚建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)公司從事“巴楚網(wǎng)站設(shè)計(jì)”,“巴楚網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。

線程池參數(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:
 *         {@code corePoolSize < 0}
 *         {@code keepAliveTime < 0}
 *         {@code maximumPoolSize <= 0}
 *         {@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 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ù)

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

  3. 當(dāng)前活躍線程數(shù)>corePoolSize,且隊(duì)列(workQueue)已滿,且當(dāng)前活躍線程數(shù)

  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類型的,而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)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!


當(dāng)前標(biāo)題:ThreadPoolExecutor線程池的示例分析
網(wǎng)站網(wǎng)址:http://fisionsoft.com.cn/article/jhdhsc.html