最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ThreadPoolExecutor參數(shù)的用法及說明

 更新時間:2023年03月14日 16:56:32   作者:晨曦下的微笑  
這篇文章主要介紹了ThreadPoolExecutor參數(shù)的用法及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

ThreadPoolExecutor參數(shù)說明

一、ThreadPoolExecutor核心參數(shù)說明

1、corePoolSize:核心線程數(shù)

* 核心線程會一直存活,及時沒有任務(wù)需要執(zhí)行

* 當線程數(shù)小于核心線程數(shù)時,即使有線程空閑,線程池也會優(yōu)先創(chuàng)建新線程處理

* 設(shè)置allowCoreThreadTimeout=true(默認false)時,核心線程會超時關(guān)閉

2、queueCapacity:任務(wù)隊列容量(阻塞隊列)

* 當核心線程數(shù)達到最大時,新任務(wù)會放在隊列中排隊等待執(zhí)行

3、maxPoolSize:最大線程數(shù)

* 當線程數(shù)>=corePoolSize,且任務(wù)隊列已滿時。線程池會創(chuàng)建新線程來處理任務(wù)

* 當線程數(shù)=maxPoolSize,且任務(wù)隊列已滿時,線程池會拒絕處理任務(wù)而拋出異常

4、 keepAliveTime:線程空閑時間

* 當線程空閑時間達到keepAliveTime時,線程會退出,直到線程數(shù)量=corePoolSize

* 如果allowCoreThreadTimeout=true,則會直到線程數(shù)量=0

5、allowCoreThreadTimeout:允許核心線程超時

6、rejectedExecutionHandler:任務(wù)拒絕處理器

* 兩種情況會拒絕處理任務(wù):

  • - 當線程數(shù)已經(jīng)達到maxPoolSize,切隊列已滿,會拒絕新任務(wù)
  • - 當線程池被調(diào)用shutdown()后,會等待線程池里的任務(wù)執(zhí)行完畢,再shutdown。如果在調(diào)用shutdown()和線程池真正shutdown之間提交任務(wù),會拒絕新任務(wù)

* 線程池會調(diào)用rejectedExecutionHandler來處理這個任務(wù)。如果沒有設(shè)置默認是AbortPolicy,會拋出異常

* ThreadPoolExecutor類有幾個內(nèi)部實現(xiàn)類來處理這類情況:

  • - AbortPolicy 丟棄任務(wù),拋運行時異常
  • - CallerRunsPolicy 執(zhí)行任務(wù)
  • - DiscardPolicy 忽視,什么都不會發(fā)生
  • - DiscardOldestPolicy 從隊列中踢出最先進入隊列(最后一個執(zhí)行)的任務(wù)

* 實現(xiàn)RejectedExecutionHandler接口,可自定義處理器

二、ThreadPoolExecutor執(zhí)行順序

線程池按以下行為執(zhí)行任務(wù)

1. 當線程數(shù)小于核心線程數(shù)時,創(chuàng)建線程。

2. 當線程數(shù)大于等于核心線程數(shù),且任務(wù)隊列未滿時,將任務(wù)放入任務(wù)隊列。

3. 當線程數(shù)大于等于核心線程數(shù),且任務(wù)隊列已滿

  • -1 若線程數(shù)小于最大線程數(shù),創(chuàng)建線程
  • -2 若線程數(shù)等于最大線程數(shù),拋出異常,拒絕任務(wù)

三、ThreadPoolExecutor如何設(shè)置參數(shù)

1、默認值

* corePoolSize=1 * queueCapacity=Integer.MAX_VALUE * maxPoolSize=Integer.MAX_VALUE * keepAliveTime=60s * allowCoreThreadTimeout=false * rejectedExecutionHandler=AbortPolicy()

2、如何來設(shè)置

* 需要根據(jù)幾個值來決定

  • - tasks :每秒的任務(wù)數(shù),假設(shè)為1000
  • - taskcost:每個任務(wù)花費時間,假設(shè)為0.1s
  • - responsetime:系統(tǒng)允許容忍的最大響應(yīng)時間,假設(shè)為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í)行

* 切記不能設(shè)置為Integer.MAX_VALUE,這樣隊列會很大,線程數(shù)只會保持在corePoolSize大小,當任務(wù)陡增時,不能新開線程來執(zhí)行,響應(yīng)時間會隨之陡增。

- maxPoolSize = (max(tasks)- queueCapacity)/(1/taskcost)

* 計算可得 maxPoolSize = (1000-corePoolSize)/10,即(每秒并發(fā)數(shù)-corePoolSize大小) / 10

* (最大任務(wù)數(shù)-隊列容量)/每個線程每秒處理能力 = 最大線程數(shù)

  • - rejectedExecutionHandler:根據(jù)具體情況來決定,任務(wù)不重要可丟棄,任務(wù)重要則要利用一些緩沖機制來處理
  • - keepAliveTime和allowCoreThreadTimeout采用默認通常能滿足

ThreadPoolExecutor參數(shù)allowCoreThreadTimeOut

ThreadPoolExecutor的執(zhí)行流程有一點可能被吐槽過,就是只有緩存隊列已經(jīng)滿了的時候才會使用到maxPoolSize創(chuàng)建新的線程.也就是說如果corePoolSize設(shè)為0的時候,要等到隊列滿了,才會創(chuàng)建線程去執(zhí)行任務(wù)

之前有被問到,希望沒有任務(wù)的時候線程池里的線程可以停掉??赡軐π阅芎唾Y源有過考慮的人都會想到這個問題吧

今天看JDK源碼的時候發(fā)現(xiàn)了ThreadPoolExecutor在1.6的時候已經(jīng)支持了

allowCoreThreadTimeOut參數(shù)就是為此設(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ù)量會一直保持,即使這些線程是空閑的,除非設(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最多只能有一個任務(wù)在隊列里面

也就是說這個線程池的任務(wù)會被立即分配一個線程去處理,如果沒有空閑的線程會立即創(chuàng)建線程。

在空閑的時候,線程數(shù)量會減少直至為0,這一點倒是滿足了要求??墒顷犃兄凶疃嘀粫彺嬉粋€任務(wù),當任務(wù)的處理速度慢于任務(wù)進入線程池的速度時,線程數(shù)量就會不斷膨脹。如果maxPoolSize設(shè)置成一個比較小的數(shù)字時,可能就會有大量任務(wù)被拒絕策略處理。

所以正如注釋中所說,newCachedThreadPool只適合于任務(wù)處理速度很快的場景下。比如做一些計算,不需要依賴其它服務(wù)

/**
     * 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)文章

  • review引發(fā)的有關(guān)于單例模式的思考

    review引發(fā)的有關(guān)于單例模式的思考

    一次代碼調(diào)試中發(fā)現(xiàn)一個情況,即我在查看memcached的connection時,發(fā)現(xiàn)總是維持在100來個左右,當然這看似沒什么問題,因為memcached默認connection有1024個。
    2013-04-04
  • SpringCloud客戶端的負載均衡Ribbon的實現(xiàn)

    SpringCloud客戶端的負載均衡Ribbon的實現(xiàn)

    微服務(wù)架構(gòu),不可避免的存在單個微服務(wù)有多個實例,這篇文章主要介紹了SpringCloud客戶端的負載均衡Ribbon的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • java 中內(nèi)部類的實例詳解

    java 中內(nèi)部類的實例詳解

    這篇文章主要介紹了java 中內(nèi)部類的實例詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • java 使用ImageIO.writer從BufferedImage生成jpeg圖像遇到問題總結(jié)及解決

    java 使用ImageIO.writer從BufferedImage生成jpeg圖像遇到問題總結(jié)及解決

    這篇文章主要介紹了java 使用ImageIO.writer從BufferedImage生成jpeg圖像遇到問題總結(jié)及解決的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Java實現(xiàn)批量發(fā)送帶附件的郵件代碼

    Java實現(xiàn)批量發(fā)送帶附件的郵件代碼

    大家好,本篇文章主要講的是Java實現(xiàn)批量發(fā)送帶附件的郵件代碼,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01
  • Springboot使用pdfbox提取PDF圖片的代碼示例

    Springboot使用pdfbox提取PDF圖片的代碼示例

    PDFBox是一個用于創(chuàng)建和處理PDF文檔的Java庫,它可以使用Java代碼創(chuàng)建、讀取、修改和提取PDF文檔中的內(nèi)容,本文就給大家介紹Springboot如何使用pdfbox提取PDF圖片,感興趣的同學(xué)可以借鑒參考
    2023-06-06
  • java線程的run()沒有返回值怎么辦?

    java線程的run()沒有返回值怎么辦?

    java線程的run()沒有返回值怎么辦?本文給出了java線程的run()返回值為空的解決辦法,感興趣的小伙伴們可以參考一下
    2016-01-01
  • Java 多線程死鎖的產(chǎn)生以及如何避免死鎖

    Java 多線程死鎖的產(chǎn)生以及如何避免死鎖

    這篇文章主要介紹了Java 多線程死鎖的產(chǎn)生以及如何避免死鎖,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Java異常之圖書管理系統(tǒng)

    Java異常之圖書管理系統(tǒng)

    這篇文章主要為大家詳細介紹了Java異常之圖書管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • java?String到底有多長?String超出長度該如何解決

    java?String到底有多長?String超出長度該如何解決

    在Java中,由于字符串常量池的存在,String常量長度限制取決于String常量在常量池中的存儲大小,下面這篇文章主要給大家介紹了關(guān)于java?String到底有多長?String超出長度該如何解決的相關(guān)資料,需要的朋友可以參考下
    2023-01-01

最新評論

陆川县| 高邮市| 甘南县| 淮阳县| 武平县| 梁平县| 罗山县| 内黄县| 筠连县| 伊春市| 曲周县| 望谟县| 大田县| 常州市| 资溪县| 滨海县| 新田县| 朔州市| 永康市| 同仁县| 山东| 呼伦贝尔市| 江西省| 丰都县| 通渭县| 大同县| 新安县| 韶山市| 伊川县| 綦江县| 清河县| 金山区| 耒阳市| 南丹县| 英吉沙县| 图木舒克市| 阳新县| 梓潼县| 黄大仙区| 无锡市| 东阿县|