public void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
/*
* Proceed in 3 steps:
*
* 1. If fewer than corePoolSize threads are running, try to
* start a new thread with the given command as its first
* task. The call to addWorker atomically checks runState and
* workerCount, and so prevents false alarms that would add
* threads when it shouldn't, by returning false.
*
* 2. If a task can be successfully queued, then we still need
* to double-check whether we should have added a thread
* (because existing ones died since last checking) or that
* the pool shut down since entry into this method. So we
* recheck state and if necessary roll back the enqueuing if
* stopped, or start a new thread if there are none.
*
* 3. If we cannot queue task, then we try to add a new
* thread. If it fails, we know we are shut down or saturated
* and so reject the task.
*/
int c = ctl.get();//ctl是一个原子类,共32位,获取它的值
if (workerCountOf(c) < corePoolSize) {//当工作线程数<核心线程数
if (addWorker(command, true))//尝试创建一个核心线程去执行当前任务,如果成功执行任务会返回true
return;
c = ctl.get();//如果没有成功执行任务,重新获取ctl
}
if (isRunning(c) && workQueue.offer(command)) {//如果线程池处于running状态,尝试将任务添加到任务队列
int recheck = ctl.get();//重新判断线程池状态
if (! isRunning(recheck) && remove(command))//如果线程池不是running状态,尝试将刚才的任务从队列中移除
reject(command);//拒绝策略
else if (workerCountOf(recheck) == 0)//如果工作线程数为0
addWorker(null, false);//创建非核心线程
}
else if (!addWorker(command, false))//如果将任务添加到工作队列失败,则创建非核心线程去执行任务
reject(command);//线程池满了,执行拒绝策略
}
/**
* The main pool control state, ctl, is an atomic integer packing
* two conceptual fields
* workerCount, indicating the effective number of threads
* runState, indicating whether running, shutting down etc
* * In order to pack them into one int, we limit workerCount to
* (2^29)-1 (about 500 million) threads rather than (2^31)-1 (2
* billion) otherwise representable. If this is ever an issue in
* the future, the variable can be changed to be an AtomicLong,
* and the shift/mask constants below adjusted. But until the need
* arises, this code is a bit faster and simpler using an int.
* * The workerCount is the number of workers that have been
* permitted to start and not permitted to stop. The value may be
* transiently different from the actual number of live threads,
* for example when a ThreadFactory fails to create a thread when
* asked, and when exiting threads are still performing
* bookkeeping before terminating. The user-visible pool size is
* reported as the current size of the workers set.
**/
private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));//ctl
private static final int COUNT_BITS = Integer.SIZE - 3;
private static final int CAPACITY = (1 << COUNT_BITS) - 1;
// runState is stored in the high-order bits
private static final int RUNNING = -1 << COUNT_BITS;
private static final int SHUTDOWN = 0 << COUNT_BITS;
private static final int STOP = 1 << COUNT_BITS;
private static final int TIDYING = 2 << COUNT_BITS;
private static final int TERMINATED = 3 << COUNT_BITS;
// Packing and unpacking ctl
private static int runStateOf(int c) { return c & ~CAPACITY; }
private static int workerCountOf(int c) { return c & CAPACITY; }
private static int ctlOf(int rs, int wc) { return rs | wc; }
ctl是一个原子类,它的注释大致意思是,ctl表示有效线程数以及线程状态。为了能用一个int类型表示这两个值,将ctl划分为前3位和后29位(ctl一共32位),前3位表示线程状态,后29位表示有效线程数。
runStateOf()方法就是用来获取线程状态的,workerCountOf()用来获取有效线程数。这两个方法都是进行与操作。