老爷们,先上图。。。。。。
根据上图以及源码核心方法解析
public void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
int c = ctl.get();
if (workerCountOf(c) < corePoolSize) {
if (addWorker(command, true))
return;
c = ctl.get();
}
if (isRunning(c) && workQueue.offer(command)) {
int recheck = ctl.get();
if (! isRunning(recheck) && remove(command))
reject(command);
else if (workerCountOf(recheck) == 0)
addWorker(null, false);
}
else if (!addWorker(command, false))
reject(command);
}
////////
取得当前线程数与线程池corePoolSize比较,如果时小于,将通过addWorker()方法调度执行,直接创建一个线程并且执行
如果大于的话,那么提交到等待队列中
如果进入等待队列失败,则会将任务直接提交给线程池
如果线程数达到最大线程数,那么就提交失败,执行拒绝策略