一日一技 (3) 如何创建线程池ThreadPoolExecutor

阿里开发规范不要显示创建线程池,也不要用excutors静态方法创建线程池,而是用ThreadPoolExecutor。

使用示例:

static ThreadFactory myThreadFactory = new
ThreadFactory() {
     
    @Override
    public Thread newThread(Runnable runnable) {
     
        return new Thread(runnable);
    }
};

public static ExecutorService pool = new ThreadPoolExecutor(1, 12,
        0L, TimeUnit.MILLISECONDS,
        new LinkedBlockingQueue<Runnable>(1024), myThreadFactory, new ThreadPoolExecutor.AbortPolicy());

ThreadPoolExecutor构造方法中各个参数的含义:

corePoolSize:线程池拥有的线程数量,不管空闲不空闲一直都在的。
maximumPoolSize: 线程池中允许的最大线程数量。
keepAliveTime:当线程池中线程最大的闲置时间,到了限制时间会销毁非核心线程。
unit:闲置时间单位,秒,分,时。
workQueue:任务无法获得线程进行处理时进入该队列等待。
threadFactory:线程工厂,具体的创建线程的方法,开放创建线程时操作。
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:
* {@code corePoolSize < 0}
* {@code keepAliveTime < 0}
* {@code maximumPoolSize <= 0}
* {@code maximumPoolSize < corePoolSize} * @throws NullPointerException if {@code workQueue} * or {@code threadFactory} or {@code handler} is null */

你可能感兴趣的:(Java一日一技,java,多线程,thread,队列,并发编程)