本人菜鸟一只,最近看了一下ThreadPoolExecutor 类的 execute() 方法。
发现 程序中显示判断 if (runState == RUNNING && workQueue.offer(command))
在这个判断的里面有写了一个if (runState != RUNNING || poolSize == 0)
如果第一个IF 条件成立 那么runState != RUNNING 就永远都不会成立了啊。。。。。。。请大牛们指点一二。。。。
runState变量在类中是这样定义的。volatile int runState;
ThreadPoolExecutor 类的 execute() 方法源代码如下:
public void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
if (poolSize >= corePoolSize || !addIfUnderCorePoolSize(command)) {
if (runState == RUNNING && workQueue.offer(command)) {
if (runState != RUNNING || poolSize == 0)
ensureQueuedTaskHandled(command);
} else if (!addIfUnderMaximumPoolSize(command))
reject(command); // is shutdown or saturated
}
}