线程池的execute方法
public void execute(Runnable command) {
// 健壮性判断,非空校验
if (command == null) throw new NullPointerException();
// 获取到核心参数ctl int c = ctl.get();
// 获取当前工作线程数量,判断是否小于核心线程数
if (workerCountOf(c) < corePoolSize) {
// 添加一个核心线程去执行任务
if (addWorker(command, true))
// 如果核心线程添加成功,返回true,添加失败返回false // 结束!
return;
// 添加核心线程失败,因为ctl有变化,需要重新获取
ctl c = ctl.get();
}
// 判断是否是RUNNING,将任务添加到工作队列中排队。
if (isRunning(c) && workQueue.offer(command)) {
// 成功添加任务到队列后,重新获取ctl int recheck = ctl.get(); // 如果不是RUNNING,将任务从队列移除
if (!isRunning(recheck) && remove(command))
// 走拒绝策略 reject(command); // 如果是RUNNING状态,判断是否工作线程数为0
else if (workerCountOf(recheck) == 0)
// 添加一个空任务线程,(非核心线程)