Java Interview Questions

Java ThreadPoolExecutor: How to Tune the 7 Parameters

Explain every parameter of ThreadPoolExecutor. When a task is submitted, what happens?

Why Interviewers Ask This

Executors.newFixedThreadPool hides traps (unbounded queue → OOM). Senior Java interviews want you to roll your own pool and justify each parameter.

How to Answer

  1. 1

    Seven parameters: corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler.

  2. 2

    Submit flow: (1) workers < core → create new worker; (2) core full → enqueue; (3) queue full → create new worker up to max; (4) max reached → rejection handler.

  3. 3

    Queue choice matters: LinkedBlockingQueue (unbounded by default, skips step 3 forever — OOM risk), ArrayBlockingQueue (bounded), SynchronousQueue (no buffering, forces direct handoff).

  4. 4

    Four built-in rejection policies: AbortPolicy (throw), CallerRunsPolicy (run on submitting thread — backpressure), DiscardPolicy (silently drop), DiscardOldestPolicy.

  5. 5

    Sizing: CPU-bound ~ N_cpu + 1. I/O-bound ~ N_cpu × (1 + wait/compute). Always monitor queue depth and active count in prod.

Sample Answer

I always create pools with new ThreadPoolExecutor, never Executors.newFixedThreadPool — that one uses an unbounded LinkedBlockingQueue which means maxPoolSize is dead code and memory grows until OOM. For a web server I typically go SynchronousQueue + large maxPoolSize + CallerRunsPolicy, so when we're overloaded submitters feel the backpressure instead of piling work. I set a named ThreadFactory so stack traces and logs show which pool a thread came from — huge for debugging. And I always wire up monitoring on getQueue().size() and getActiveCount() before prod.

Pro Tips

  • Tomcat uses a custom TaskQueue that flips the order — prefer growing threads up to max before queueing.

  • Avoid Executors.newCachedThreadPool in uncontrolled environments — max is Integer.MAX_VALUE.

  • HireMe AI (即答侠) drills the "submit flow" with callbacks so you can recite the four branches without stumbling.

Frequently Asked Questions

corePoolSize threads die when idle?

No by default. Set allowCoreThreadTimeOut(true) to let core threads die after keepAliveTime too.

What if a task throws?

Uncaught exceptions kill the worker; the pool replaces it. Always wrap tasks in try/catch or submit Futures and inspect Future.get().

Worried About Going Blank in Your Interview?

HireMe AI listens to your interview in real-time, detects questions, and generates instant answer suggestions — so you never freeze up again.

Try HireMe AI Free