Java-ThreadPoolExecutor類

ThreadPoolExecutor提供了四種構造方法:
java

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類構建,不過須要類型強轉以及手動去配置一些屬性。緩存

Java經過Executors提供四種線程池,分別爲:
newCachedThreadPool建立一個可緩存線程池,若是線程池長度超過處理須要,可靈活回收空閒線程,若無可回收,則新建線程。
newFixedThreadPool 建立一個定長線程池,可控制線程最大併發數,超出的線程會在隊列中等待。
newScheduledThreadPool 建立一個定長線程池,支持定時及週期性任務執行。
newSingleThreadExecutor 建立一個單線程化的線程池,它只會用惟一的工做線程來執行任務,保證全部任務按照指定順序(FIFO, LIFO, 優先級)執行。

對於構造函數中的參數:併發

        corePoolSize:正常運行時線程量。less

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

        keepAliveTime:線程的生命時長spa

        unit:與keepAliveTime對應表明時間的單位線程

        workQueue:隊列code

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

        threadFactory:線程建立工廠get

        說明:

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

handler有四種處理方式:

static class 	ThreadPoolExecutor.AbortPolicy
A handler for rejected tasks that throws a RejectedExecutionException.
static class 	ThreadPoolExecutor.CallerRunsPolicy
A 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.DiscardOldestPolicy
A 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.DiscardPolicy
A handler for rejected tasks that silently discards the rejected task.

        AbortPolicy:拋出異常給調用者

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

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

        DiscardPolicy:拋棄任務

部分方法說明:

        execute:執行任務

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

        getPoolSize:獲取當前線程池數量

        getQueue:獲取等待隊列

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

相關文章
相關標籤/搜索