Q47: How do you tune custom ThreadPoolExecutor parameters?

Constructor signature: ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler)

  • Execution Flow:
    1. If active threads < corePoolSize, create new thread.
    2. If active threads $ge$ corePoolSize, queue task in workQueue.
    3. If workQueue is full AND active threads < maximumPoolSize, create new thread.
    4. If workQueue is full AND active threads = maximumPoolSize, trigger RejectedExecutionHandler (AbortPolicy, CallerRunsPolicy).
Key Takeaways
  • Tasks land in workQueue BEFORE worker count expands from corePoolSize to maximumPoolSize.
Knowledge Check

When does ThreadPoolExecutor begin spawning threads beyond corePoolSize up to maximumPoolSize?