Q47: How do you tune custom ThreadPoolExecutor parameters?
Constructor signature: ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler)
- Execution Flow:
- If active threads <
corePoolSize, create new thread. - If active threads $ge$
corePoolSize, queue task inworkQueue. - If
workQueueis full AND active threads <maximumPoolSize, create new thread. - If
workQueueis full AND active threads =maximumPoolSize, triggerRejectedExecutionHandler(AbortPolicy,CallerRunsPolicy).
- If active threads <
Key Takeaways
- Tasks land in
workQueueBEFORE worker count expands fromcorePoolSizetomaximumPoolSize.
Knowledge Check
When does ThreadPoolExecutor begin spawning threads beyond corePoolSize up to maximumPoolSize?