Java ExecutorService — Thread Pool Types
Fast recall
Quick revision
Core answer
Knowledge of the standard pool types and awareness of the hidden dangers (unbounded queues, unbounded thread creation) in factory defaults.
Using newCachedThreadPool for long-running tasks — it spawns one thread per task, which can exhaust system resources under load.
Mention that Executors.newVirtualThreadPerTaskExecutor() (Java 21) is now the preferred choice for I/O-bound workloads, and that ForkJoinPool is better for CPU-bound divide-and-conquer work.
Interview-ready guide
Interview answer
Thread pool sizing follows different formulas for CPU-bound vs I/O-bound work — but the formula is a starting point; measurement under realistic load is what actually matters.
Sizing formulas:
- CPU-bound: N cores — more threads cause context switching waste, hurting throughput
- I/O-bound: N × (1 + wait/compute) — if tasks wait 90% of the time, you need 10x the cores to keep CPUs busy
Executors factory methods — avoid in production:
newCachedThreadPool(): no thread limit — creates thousands of threads under load, each consuming ~1MB stacknewFixedThreadPool(): unbounded queue — tasks accumulate indefinitely if consumers are slower than producers
Production approach:
ThreadPoolExecutorwith explicit bounded queue (ArrayBlockingQueue) and aCallerRunsPolicyrejection handlerCallerRunsPolicy: submitting thread runs the task when pool is full — natural backpressure that slows producers- Custom
ThreadFactorythat names threads for thread dump readability
Java 21 virtual threads:
- I/O-bound sizing becomes irrelevant — create one virtual thread per task; JVM manages the carrier threads
- Blocking I/O unmounts the virtual thread instead of blocking a platform thread
I recommend starting with the formula, monitoring under realistic load, and adjusting. Formula-based sizing without measurement is guessing with math.
Java ExecutorService — Thread Pool Types
Direct Answer
The Executors factory provides four main pools: fixed (predictable throughput), cached (elastic short-lived tasks), single-threaded (ordered sequential execution), and scheduled (delayed or periodic tasks). Choosing correctly depends on task duration, expected concurrency, and whether ordering matters. For production use, prefer constructing ThreadPoolExecutor directly to control queue depth and rejection policies rather than relying on factory defaults.
Deep Dive & Architecture
Thread pools solve three real production problems. (1) Thread creation is expensive — a platform thread costs roughly 1 MB of stack and tens of microseconds of pthread_create / CreateThread time.
Creating a thread per task means the OS spends more time bookkeeping than doing work. (2) Unbounded concurrency kills servers — new Thread() per request lets a traffic spike spawn 50,000 threads, exhaust memory, and OOM-crash the JVM.
The Executors factory provides four main pools: fixed (predictable throughput), cached (elastic short-lived tasks), single-threaded (ordered sequential execution), and scheduled (delayed or periodic tasks). Choosing correctly depends on task duration, expected concurrency, and whether ordering matters. For production use, prefer constructing ThreadPoolExecutor directly to control queue depth and rejection policies rather than relying on factory defaults.
Saying only 'thread creation is expensive' and stopping there — bounded concurrency and graceful shutdown are equally important. Using Executors.newCachedThreadPool() in production without understanding it's unbounded
Common Mistakes & Pitfalls
Using newCachedThreadPool for long-running tasks — it spawns one thread per task, which can exhaust system resources under load.
Check your understanding
Answer these without looking back at the article.
- Why should you use a thread pool in Java instead of creating threads directly with `new Thread()`?
- What is ForkJoinPool and how does it differ from a standard ThreadPoolExecutor? Explain the work-stealing algorithm and show a RecursiveTask example.
- Compare `FixedThreadPool`, `CachedThreadPool`, and `SingleThreadExecutor`. When is each appropriate or dangerous?
Related interview prep categories
2Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.