【Java】線程池 參數

* @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

幾個關鍵參數:

1.corePoolSize:核心線程數,將會永久存在線程池中;

2.maximumPoolSize:最大線程數,如果任務隊列滿了,新來的任務將會額外創建臨時線程來處理,但是總的線程數目不會超過這個值;

3.keepAliveTime:指的是額外創建的臨時線程過期閾值;


線程池的參數作用:如果線程數小於corePoolSize,那麼每一個任務進來都會創建線程;如果大於等於corePoolSize,那麼新來的任務將會進入隊列等待處理;如果隊列超過閾值,那麼就會新建臨時線程,直到線程數到達maximumPoolSize。如果到達maximumPoolSize,那麼會根據拒絕策略拒絕後來的任務。


線程池原本的模型就是池化一定數目的線程,然後處理用戶請求,如果用戶請求多了,那麼就扔進隊列裏,等待池化的線程處理完手頭工作再處理 新的請求。那麼如果隊列也滿了,說明系統不堪重負,此時需要臨時添加一下線程救急,一旦過了高峯期,這些臨時線程就會被回收,這就是線程池的概念模型。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章