if (value && keepAliveTime <= 0)
throw new IllegalArgumentException("Core threads must have nonzero keep alive times");
if (value != allowCoreThreadTimeOut) {
allowCoreThreadTimeOut = value;
if (value)
interruptIdleWorkers();
}
}
这个是allowCoreThreadTimeOut成员变量的setter
判断如果参数为true 且keepAliveTime小于等于0 就抛出异常
判断参数如果和allowCoreThreadTimeOut成员变量不一样 就用参数覆盖成员变量
如果参数为true就调用interruptIdleWorkers方法打断空闲worker
public void setMaximumPoolSize(int maximumPoolSize) {
if (maximumPoolSize <= 0 || maximumPoolSize < corePoolSize)
throw new IllegalArgumentException();
this.maximumPoolSize = maximumPoolSize;
if (workerCountOf(ctl.get()) > maximumPoolSize)
interruptIdleWorkers();
}
这个是maximumPoolSize的setter方法
判断如果maximumPoolSize小于等于0 或 maximumPoolSize小于corePoolSize 就抛出异常
将参数覆盖成员变量
判断如果worker数大于maximumPoolSize 就打断空闲worker的线程
public int getMaximumPoolSize() {
return maximumPoolSize;
}
这是maximumPoolSize的getter方法
public void setKeepAliveTime(long time, TimeUnit unit) {
if (time < 0)
throw new IllegalArgumentException();
if (time == 0 && allowsCoreThreadTimeOut())
throw new IllegalArgumentException("Core threads must have nonzero keep alive times");
long keepAliveTime = unit.toNanos(time);
long delta = keepAliveTime - this.keepAliveTime;
this.keepAliveTime = keepAliveTime;
if (delta < 0)
interruptIdleWorkers();
}
这是keepAliveTime的setter方法
判断如果time参数小于0 就抛出异常
判断如果time为0 且 allowsCoreThreadTimeOut为true 就抛出异常
计算两个参数 一个是时间值 一个是时间单位 得出具体keepAlive时间
计算上面得出的keepAlive时间和成员变量keepAlive的差值
将成员变量覆盖为上面计算出的keepAlive时间
判断如果差值小于0 也就是要减少worker线程存活时间 就打断空闲worker的线程
public long getKeepAliveTime(TimeUnit unit) {
return unit.convert(keepAliveTime, TimeUnit.NANOSECONDS);
}
keepAliveTime的getter方法
public BlockingQueue<Runnable> getQueue() {
return workQueue;
}
workQueue的getter方法
public boolean remove(Runnable task) {
boolean removed = workQueue.remove(task);
tryTerminate(); // In case SHUTDOWN and now empty
return removed;
}
这个是删除任务的方法 从任务队列里将任务删除
调用tryTerminate方法 中断空闲worker的线程
最后返回删除任务是否成功
ThreadPoolExecutor里workQueue是一个队列 队列里装了Runnable 也就是任务
workers是HashSet里面装了Worker