How do you configure ThreadPoolExecutor correctly for production?
Parameters and how a task is placed
new ThreadPoolExecutor(core, max, keepAlive, unit, queue, threadFactory, rejectionHandler)
- Fewer than
corethreads running → start a new thread. - Otherwise → put the task in the queue.
- Queue full → start threads up to
max. - At
maxwith a full queue → rejection policy.
So with an unbounded queue, max is never used and the queue grows until OutOfMemoryError.
Choosing the queue
- Bounded
ArrayBlockingQueue: the safe default; gives back-pressure. LinkedBlockingQueuewithout a capacity: unbounded (this is whatExecutors.newFixedThreadPooluses).SynchronousQueue: no buffering; hands off directly or creates a thread (newCachedThreadPool, which can create unbounded threads).
Rejection policies
AbortPolicy (throw, the default), CallerRunsPolicy (the submitting thread runs the task, which naturally slows producers), DiscardPolicy, DiscardOldestPolicy, or a custom handler that records a metric and returns a 503.
Sizing
- CPU-bound: about
Ncpu(orNcpu + 1). - I/O-bound:
Ncpu × (1 + wait time / compute time). With 8 cores and tasks that wait 90 ms for every 10 ms of CPU, that is 8 × 10 = 80 threads. - Validate with load tests and metrics (active threads, queue size, task latency). Also respect downstream limits: 200 threads against a 20-connection DB pool just moves the queue.
Operational hygiene
Name threads with a ThreadFactory, expose metrics (Micrometer's ExecutorServiceMetrics), propagate context (MDC, trace IDs, security) with decorators, and shut down gracefully: shutdown(), awaitTermination, then shutdownNow().
Example
ThreadPoolExecutor pool = new ThreadPoolExecutor(
16, 32, 60, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(500),
Thread.ofPlatform().name("report-worker-", 0).factory(),
new ThreadPoolExecutor.CallerRunsPolicy());Follow-up questions interviewers ask
Try answering each one before you open it.
Why are the Executors factory methods discouraged in production?
newFixedThreadPool and newSingleThreadExecutor use an unbounded LinkedBlockingQueue, so a slow consumer leads to unbounded memory growth and latency. newCachedThreadPool allows Integer.MAX_VALUE threads, so a burst can create thousands of threads and exhaust memory. Constructing ThreadPoolExecutor explicitly forces you to choose bounds and a rejection policy.
What is ForkJoinPool and work stealing?
ForkJoinPool gives each worker its own deque. A worker pushes subtasks it forks onto its own deque and pops from the same end (LIFO, good locality); idle workers steal from the other end of busy workers' deques. It suits recursive divide-and-conquer CPU work. The common pool (size Ncpu - 1) backs parallel streams and default CompletableFuture async methods, so never block in it.
What happens to exceptions thrown inside a task?
With execute(), an uncaught exception kills the worker thread (the pool replaces it) and goes to the thread's UncaughtExceptionHandler. With submit(), the exception is captured in the returned Future and is silently lost unless someone calls get(). Always handle errors inside the task or inspect the Future; you can also override afterExecute to log failures.