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

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