【发布时间】:2018-02-21 14:27:40
【问题描述】:
我看了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
}
}
但如果满足poolSize >= corePoolSize 条件,似乎什么都没有发生!
因为如果ORcondition 的第一部分为真,则不会执行第二部分:
if (true || anyMethodWillNotBeExecuted()) { ... }
根据the rules for thread creation,这里也是maximumPoolSize。如果线程数等于(或大于)corePoolSize 且小于maxPoolSize,则应为任务创建新线程或将任务添加到队列中。
那么为什么如果poolSize 大于或等于corePoolSize 什么都不应该发生呢?..
【问题讨论】:
标签: java multithreading