Concurrent ThreadPoolExecutor.submit() calls

Viewed 47

I have a ThreadPoolExecutor whose submit method may be called concurrently by multiple threads. I’m wondering how the ThreadPoolExecutor would handle this. I don’t see that submit is atomic from the source code, although maybe I’m missing something. Thanks in advance for any advice.

1 Answers

At submit() ExecutorService relies on its BlockingQueue for synchronization.

When initializing a executor you have to provide a queue instance which is used to store all submited tasks and is used as source for the thread pool to read from.

For executors initialized from one of the Executors fixed size factory methods a LinkedBlockingQueue is used internally.

LinkedBlockingQueue internally uses a ReentrantLock to lock and synchronize when it is offered new entries to store

As long as you work with java.util.concurrent implementations for your Queue concurrent submits should work just fine.

Related