Java-ThreadPoolExecutor類

來源:互聯網
上載者:User

標籤:

ThreadPoolExecutor提供了四種構造方法:

ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue)Creates a new ThreadPoolExecutor with the given initial parameters and default thread factory and rejected execution handler.ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler)Creates a new ThreadPoolExecutor with the given initial parameters and default thread factory.ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory)Creates a new ThreadPoolExecutor with the given initial parameters and default rejected execution handler.ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)Creates a new ThreadPoolExecutor with the given initial parameters.

當然也可以通過Executors類構建,不過需要類型強轉以及手動去配置一些屬性。

對於建構函式中的參數:

        corePoolSize:正常運行時線程量。

        maximumPoolSize:最大容納的線程數量。

        keepAliveTime:線程的生命時間長度

        unit:與keepAliveTime對應代表時間的單位

        workQueue:隊列

        handler:提交線程數量大於maximumPoolSize時的處理器

        threadFactory:線程建立工廠

        說明:

                如果此時線程池中的數量小於corePoolSize,即使線程池中的線程都處於空閑狀態,也要建立新的線程來處理被添加的任務。如果此時線程池中的數量等於 corePoolSize,但是緩衝隊列 workQueue未滿,那麼任務被放入緩衝隊列。如果此時線程池中的數量大於corePoolSize,緩衝隊列workQueue滿,並且線程池中的數量小於maximumPoolSize,建新的線程來處理被添加的任務。如果此時線程池中的數量大於corePoolSize,緩衝隊列workQueue滿,並且線程池中的數量等於maximumPoolSize,那麼通過 handler所指定的策略來處理此任務。也就是:處理任務的優先順序為:核心線程corePoolSize、任務隊列workQueue、最大線程maximumPoolSize,如果三者都滿了,使用handler處理被拒絕的任務。當線程池中的線程數量大於 corePoolSize時,如果某線程空閑時間超過keepAliveTime,線程將被終止。這樣,線程池可以動態調整池中的線程數。

handler有四種處理方式:

static class ThreadPoolExecutor.AbortPolicyA handler for rejected tasks that throws a RejectedExecutionException.static class ThreadPoolExecutor.CallerRunsPolicyA handler for rejected tasks that runs the rejected task directly in the calling thread of the execute method, unless the executor has been shut down, in which case the task is discarded.static class ThreadPoolExecutor.DiscardOldestPolicyA handler for rejected tasks that discards the oldest unhandled request and then retries execute, unless the executor is shut down, in which case the task is discarded.static class ThreadPoolExecutor.DiscardPolicyA handler for rejected tasks that silently discards the rejected task.

        AbortPolicy:拋出異常給調用者

        CallerRunsPolicy:在調用者所線上程執行、

        DiscardOldestPolicy:拋棄等待隊列中等待最長的那個任務,並把這個任務加入到隊列

        DiscardPolicy:拋棄任務

部分方法說明:

        execute:執行任務

        getCorePoolSize:擷取普通運行時線程數量上限

        getPoolSize:擷取當前線程池數量

        getQueue:擷取等待隊列

        getActiveCount:擷取正在執行的線程數量(估計值)



Java-ThreadPoolExecutor類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.