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 than 10 * the core, this is the maximum time that excess idle threads 11 * will wait for new tasks before terminating. 12 * @param unit the time unit for the {@code keepAliveTime} argument 13 * @param workQueue the queue to use for holding tasks before they are 14 * 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 executor 17 * creates a new thread 18 * @param handler the handler to use when execution is blocked 19 * because the thread bounds and queue capacities are reached 20 * @throws IllegalArgumentException if one of the following holds:<br> 21 * {@code corePoolSize < 0}<br> 22 * {@code keepAliveTime < 0}<br> 23 * {@code maximumPoolSize <= 0}<br> 24 * {@code maximumPoolSize < corePoolSize} 25 * @throws NullPointerException if {@code workQueue} 26 * or {@code threadFactory} or {@code handler} is null 27 */ 28 public ThreadPoolExecutor(int corePoolSize, 29 int maximumPoolSize, 30 long keepAliveTime, 31 TimeUnit unit, 32 BlockingQueue<Runnable> 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<Runnable>
用来持有执行前的任务。且这个队列只能用于被 execute 调用的 Runnable 任务。
6. threadFactory ThreadFactory
执行程序用来创建新线程使用的工厂。
7. handler RejectedExecutionHandler
用于超过线程上限或达到队列容量而致使程序阻塞时所用的处理程序。