java.util.concurrent.ThreadPoolExecutor

java.util.concurrent.ThreadPoolExecutor-public class ThreadPoolExecutor extends AbstractExecutorService

建立線程的方式:繼承Thread類(java.lang.Thread-

public class Thread implements Runnable)、實現Runnable接口(java.lang.Runnable-

public interface Runnable)

上面2種方式可實現多線程.但若併發數量多,且每個線程執行時間短,這樣頻繁創建線程會大大降低系統效率,因爲頻繁創建線程和銷燬線程需要時間.那麼有沒有一種辦法使得線程可以複用,就是執行完一個任務並不被銷燬,而是能繼續執行其他任務?線程池可以達到這樣的效果.

java.util.concurrent.ThreadPoolExecutor類是線程池中最核心的一個類,它具有4個構造函數:

public class ThreadPoolExecutor extends AbstractExecutorService {
    ...
    public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue) {
        this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
             Executors.defaultThreadFactory(), defaultHandler);
    }
    public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory) {
        this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
             threadFactory, defaultHandler);
    }
    public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              RejectedExecutionHandler handler) {
        this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
             Executors.defaultThreadFactory(), handler);
    }
    /**
     * 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:<br>
     *         {@code corePoolSize < 0}<br>
     *         {@code keepAliveTime < 0}<br>
     *         {@code maximumPoolSize <= 0}<br>
     *         {@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<Runnable> 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;
    }
    ...
}

構造器參數涵義:
corePoolSize:核心池大小.創建線程池後,默認,線程池中並無線程,而是等有任務到來才創建線程去執行任務,除非調用了prestartCoreThread()(public int prestartAllCoreThreads(){...})或prestartAllCoreThreads()(public boolean prestartCoreThread(){...}),即在無任務到來前就創建1個線程或corePoolSize個線程.默認,在創建了線程池後,線程池中線程數=0,有任務到來後,創建1個線程去執行,當線程池中的線程數達到corePoolSize後,就會把到達的任務放到緩存隊列中
maximumPoolSize:線程池最大線程數,表示在線程池中最多能創建多少個線程
keepAliveTime:表示線程沒有任務執行時最多保持多久會終止.默認,只有當線程池中的線程數>corePoolSize時,keepAliveTime才起作用,直到線程池中的線程數<=corePoolSize.但若調用了allowCoreThreadTimeOut(boolean)(public void allowCoreThreadTimeOut(boolean value){...}),線程池中的線程數<=corePoolSize時keepAliveTime也會起作用,直到線程池中的線程數=0
unit:keepAliveTime的時間單位,有7種(java.util.concurrent.TimeUnit-public enum TimeUnit):
TimeUnit.NANOSECONDS
TimeUnit.MICROSECONDS
TimeUnit.MILLISECONDS
TimeUnit.SECONDS
TimeUnit.MINUTES
TimeUnit.HOURS
TimeUnit.DAYS

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