Does it mean that the count of simultaneously running thread in a fixed-size thread pool will be always less than corePoolSize unless the queue is full?
No, it does not.
Semantic approach
As I read it, and for fixed size pools, this quote states nothing about the number of active threads specifically with respect to the size of the queue. The only sentence tying the two is this one :
If there are more than corePoolSize but less than maximumPoolSize threads running, a new thread will be created only if the queue is full.
Which does not apply, since in a fixed-size pool has corePoolSize equal to maximumPoolSize. The "if" condition is never met.
What it does state though is :
When a new task is submitted [...], and fewer than corePoolSize threads are running, a new thread is created to handle the request, even if other worker threads are idle.
Threads will be created as long as the corePoolSize limit is not hit. As long as this limit is not hit, threads will not be reused (but they may die, on uncaught exception, or through the timeout feature of the pool). Which clearly leaves room for the creation of corePoolSize threads, if we create them fast enough or have a long enough queue.
Experimental approach
This knowledge allows us to imagine this scenario : make a fixed pool of size 2, with a waiting queue of size 5, and then submit two long running tasks to the pool. ("Long running" meaning the execution time of each task is one order of magnitude greater than the time it takes for the main thread to submit them, and for the thread pool to ackowledge their presence and work on them). A possible schedule for this is the following :
- The main thread submits task T1 to the new, empty, pool
- By virtue of the above quote, the
corePoolSize not being hit yet, a new thread is created to execute T1.
- The thread 1 starts executing T1
- The main thread submits T2
- As in step 2, a second thread is spawn, and the pool reaches
corePoolSize
- As in step 3, the second thread starts executing task T2
At this point, the thread pool has en empty queue, and a running thread count of exactly corePoolSize, and not "under corePoolSize", QED.
What it does mean :
Kind of the other way around, it means, the only way to get a number of thread greater than corePoolSize is having allconditions met at the same time :
- a number of running threads greater than (or equal to)
corePoolSize, and
maximumPoolSize greater than corePoolSize, and
- a full queue
maximumPoolSizethreads have started to handle a full queue, but no new tasks come in. As tasks complete, you will reach a point where thecorePoolSize < numberOfThreadsRunning < maximumPoolSize. - bradimuscorePoolSizebut less thanmaximumPoolSizethreads running ifcorePoolSizeis not strictly less thanmaximumPoolSize? - bradimus