java 线程池参数
创建线程池的构造函数如下:
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueueworkQueue, 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.acc = System.getSecurityManager() == null ? null : AccessController.getContext(); this.corePoolSize = corePoolSize; this.maximumPoolSize = maximumPoolSize; this.workQueue = workQueue; this.keepAliveTime = unit.toNanos(keepAliveTime); this.threadFactory = threadFactory; this.handler = handler; }
| corePoolSize | 核心线程数,线程池中最小线程数,即使线程空闲,也不会被销毁;任务首先提交到核心线程中执行。(初始化时不创建线程,提交任务时创建线程)(如果设置allowCoreThreadTimeOut为true,核心线程也会被销毁,使用较少) |
| maximumPoolSize | 线程池中允许创建的最大线程数。 |
| keepAliveTime | 当线程数大于corePoolSize时,线程最多等待keepAliveTime时间后,将被销毁。 |
| unit | keepAliveTime参数的时间单位,一般为毫秒。 |
| workQueue | 提交任务的缓存队列。 |
| threadFactory | 创建线程的工厂。 |
| handler | 当corePoolSize已满,缓存队列已满,maximumPoolSize已满时,又发生任务提交时的处理器。 |