开发者

java.util.concurrent.ThreadPoolExecutor strange logic

开发者 https://www.devze.com 2022-12-24 14:41 出处:网络
Look ath this method of ThreadPoolExcecutor: public void execute(Runnable command) { ... if (runState == RUNNING && workQueue.offer(command)) {

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.

Do you think this code is correct?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消