if (threadFactory == null)
throw new NullPointerException();
this.threadFactory = threadFactory;
}
public ThreadFactory getThreadFactory() {
return threadFactory;
}
这个是线程工厂的setter和getter方法 判断参数为空就抛出异常
public void setRejectedExecutionHandler(RejectedExecutionHandler handler) {
if (handler == null)
throw new NullPointerException();
this.handler = handler;
}
public RejectedExecutionHandler getRejectedExecutionHandler() {
return handler;
}
这个是拒绝策略的setter和getter方法
public void setCorePoolSize(int corePoolSize) {
if (corePoolSize < 0)
throw new IllegalArgumentException();
int delta = corePoolSize - this.corePoolSize;
this.corePoolSize = corePoolSize;
if (workerCountOf(ctl.get()) > corePoolSize)
interruptIdleWorkers();
else if (delta > 0) {
int k = Math.min(delta, workQueue.size());
while (k-- > 0 && addWorker(null, true)) {
if (workQueue.isEmpty())
break;
}
}
}
这个是设置corePoolSize的方法
判断如果参数小于0 就抛出异常
创建临时变量delta值为参数和成员变量corePoolSize的差
将成员变量corePoolSize赋为参数
判断如果worker数目大于参数
调用interruptIdleWorkers方法打断空闲worker的线程
如果worker数目不大于参数 并且delta大于0 也就是参数比原来的corePoolSize大
取delta和workQueue长度的最小值
进入while循环 添加空闲worker 使空worker数等于成员变量corePoolSize
public int getCorePoolSize() {
return corePoolSize;
}
corePoolSize的getter方法
public boolean prestartCoreThread() {
return workerCountOf(ctl.get()) < corePoolSize &&
addWorker(null, true);
}
这个是预先启动核心线程的方法
返回worker数是否小于corePoolSize 且 添加空worker是否返回true
void ensurePrestart() {
int wc = workerCountOf(ctl.get());
if (wc < corePoolSize)
addWorker(null, true);
else if (wc == 0)
addWorker(null, false);
}
这个是确保可以预先启动的方法
先获取worker数wc 判断worker数如果小于corePoolSize 就添加一个空worker addWorker方法core参数传入true
如果worker数为0 添加空worker addWorker方法core参数为false
public int prestartAllCoreThreads() {
int n = 0;
while (addWorker(null, true))
++n;
return n;
}
预先启动所有的核心线程的方法
创建临时变量n
进入while循环 循环条件是addWorker方法添加空worker的返回值为true
n自增 最后返回n
public boolean allowsCoreThreadTimeOut() {
return allowCoreThreadTimeOut;
}
是否允许核心线程超时的方法 就是allowCoreThreadTimeOut的getter