ThreadPoolExecutor参数详解
不多说,先看源码
1 /**
2 * Creates a new {@code ThreadPoolExecutor} with the given initial 3 * parameters. 4 *
5 * @param corePoolSize the number of threads to keep in the pool, even 6 * if they are idle, unless {@code allowCoreThreadTimeOut} is set
7 * @param maximumPoolSize the maximum number of threads to allow in the 8 * pool
9 * @param keepAliveTime when the number of threads is greater than10 * the core, this is the maximum time that excess idle threads11 * will wait for new tasks before terminating.
12 * @param unit the time unit for the {@code keepAliveTime} argument13 * @param workQueue the queue to use for holding tasks before they are14 * executed. This queue will hold only the {@code Runnable}15 * tasks submitted by the {@code execute} method.
16 * @param threadFactory the factory to use when the executor17 * creates a new thread
18 * @param handler the handler to use when execution is blocked19 * because the thread bounds and queue capacities are reached20 * @throws IllegalArgumentException if one of the following holds:
21 * {@code corePoolSize < 0}
22 * {@code keepAliveTime < 0}
23 * {@code maximumPoolSize <= 0}
24 * {@code maximumPoolSize < corePoolSize}25 * @throws NullPointerException if {@code workQueue}26 * or {@code threadFactory} or {@code handler} is null27 */
28 public ThreadPoolExecutor(int corePoolSize,29 int maximumPoolSize,30 long keepAliveTime,31 TimeUnit unit,
32 BlockingQueue workQueue,33 ThreadFactory threadFactory,34 RejectedExecutionHandler handler) {35 if (corePoolSize < 0 ||
36 maximumPoolSize <= 0 ||
37 maximumPoolSize < corePoolSize ||38 keepAliveTime < 0)
39 throw new IllegalArgumentException();
40 if (workQueue == null || threadFactory == null || handler == null)41 throw new NullPointerException();42 this.corePoolSize = corePoolSize;
43 this.maximumPoolSize = maximumPoolSize;44 this.workQueue = workQueue;
45 this.keepAliveTime = unit.toNanos(keepAliveTime);46 this.threadFactory = threadFactory;47 this.handler = handler;48 }
稍微翻译⼀下1. corePoolSize int
线程池中存在的线程数量(即使线程空闲),除⾮设置了allowCoreThreadTimeOut。2. maximumPoolSize int
线程池中允许的最⼤线程数量。3. keepAliveTime long
线程池中若线程数量超过 corePoolSize ,终⽌多余的空闲线程等待新任务的最长时间。4. unit TimeUnit
参数 keepAliveTime 的时间单位。5. workQueue BlockQueue ⽤来持有执⾏前的任务。且这个队列只能⽤于被 execute 调⽤的 Runnable 任务。6. threadFactory ThreadFactory
执⾏程序⽤来创建新线程使⽤的⼯⼚。
7. handler RejectedExecutionHandler
⽤于超过线程上限或达到队列容量⽽致使程序阻塞时所⽤的处理程序。