1

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 wanted to understand the internals of the thread pool.
Do you think this code is correct?

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

runState is volatile, so yes its state can change after initial check and a call to workQueue.offer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.