java.util.concurrent.ThreadPoolExecutor strange logic
Look ath this method of ThreadPoolExcecutor:
public void execute(Runnable command) {
...
if (runState == RUNNING && workQueue.offer(command)) {
if (runState != RUNNING || poolSize == 0)
ensureQueuedTaskHandled(command);
}
...
}
It check that runState
is RUNNING
and then the oposite. As I'm trying to do some tuning on a SEDA like model I want开发者_运维问答ed to understand the internals of the thread pool.
The executor's runState can potentially change between the initial check and the subsequent check after the command is added to the queue. One reason for this is if the executor's shutdown()
method is called.
The second check is performed so that if the executor has been shut-down then any queued tasks are handled appropriately rather than being stranded on the queue.
runState
is volatile
, so yes its state can change after initial check and a call to workQueue.offer
精彩评论