views:

80

answers:

2

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  A: 

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

Alexander Pogrebnyak
+2  A: 

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.

Adamski