Executors and thread pools
28 questions in 8 subtopics.
ExecutorService
What doExecutors.newFixedThreadPool, newCachedThreadPool and newSingleThreadExecutor create, and what hidden risk does each carry?This code calls three services and is slow:List<Future<R>> fs = new ArrayList<>();
for (Call c : calls) {
Future<R> f = pool.submit(c);
fs.add(f);
f.get();
}
invokeAll and invokeAny.Thread count on a service grows by several hundred per hour until it hits limits. A heap dump shows many ThreadPoolExecutor instances. Code search finds:public Report build() {
ExecutorService ex = Executors.newFixedThreadPool(8);
... return report;
}
ThreadPoolExecutor parameters
Explain the order in whichThreadPoolExecutor uses core threads, the queue and extra threads when tasks arrive. What happens with new ThreadPoolExecutor(2, 10, 60, SECONDS, new LinkedBlockingQueue<>()) when 1,000 tasks are submitted at once?What do allowCoreThreadTimeOut, prestartAllCoreThreads and keepAliveTime do, and how does Tomcat's executor change the default queue-before-grow behaviour?Design the thread pool configuration for a service that calls a payment provider with p50 latency 80 ms and p99 600 ms, at peak 500 calls per second, where the provider allows at most 200 concurrent connections from you.Fifty teams create thread pools in different ways: Executors factory methods, hand-built ThreadPoolExecutors and Spring task executors. Define the thread-pool conventions you would provide to all teams and how you would get them adopted.Rejection policies
List the built-inThreadPoolExecutor rejection policies and when each fits.A web service submits background tasks to a bounded pool configured with CallerRunsPolicy. During a slowdown, request latency for unrelated endpoints spikes and the server stops accepting connections. Explain and propose an alternative.Define an overload-handling standard for all services in your platform covering executor rejection, request queues and responses to clients.Pool sizing
How do you estimate a thread pool size for CPU-bound work and for I/O-bound work, and what doesRuntime.availableProcessors() report in a container?A service has a 200-thread pool calling the database and a Hikari connection pool of 20. Under load, most threads are waiting in getConnection and latency is high. A teammate suggests raising the thread pool to 400. What do you recommend?Services in your platform each call several dependencies through one shared pool, and a slow dependency regularly exhausts the pool and takes down unrelated features. What isolation and sizing standard would you introduce?Across your fleet, thread pool and connection pool sizes were chosen by guesswork years ago, leading to both over-provisioned instances and recurring saturation incidents. How would you make concurrency capacity decisions fleet-wide for the coming years, given virtual threads?ScheduledExecutorService
What is the difference betweenscheduleAtFixedRate and scheduleWithFixedDelay, and what happens if a scheduled task throws an exception?A cache-refresh task scheduled with scheduleAtFixedRate every minute stopped running after a few hours. There are no errors in the logs and the application is otherwise healthy. What happened and how do you prevent it?A service runs a nightly billing job with ScheduledExecutorService. After scaling to three instances, customers are billed three times. Compare options to fix this.ForkJoinPool and work stealing
What is the difference betweenForkJoinPool and a regular ThreadPoolExecutor, and when would you choose each?How does ForkJoinPool work stealing work, and why does it suit divide-and-conquer tasks?Write a RecursiveTask<Long> that sums a long[] array, and point out the common mistake in combining subtasks.ForkJoinPool now sits behind parallel streams, the default executor of CompletableFuture, and the virtual thread scheduler. Why did the JDK build so much on one pool design, and what risks does that concentration bring?Common pool pitfalls
A service usesCompletableFuture.supplyAsync(() -> restClient.get(url)) without an executor and parallel streams elsewhere. Under load, both become very slow. Explain.A request handler sorts and filters large lists with parallelStream(). At low traffic it is fast, but at high traffic latency is worse than with sequential streams. Why, and what would you recommend?Several incidents across teams were caused by blocking calls on ForkJoinPool.commonPool via CompletableFuture and parallel streams. What policy and tooling would you introduce?ForkJoinPool.commonPool() was introduced in Java 8 as a JVM-wide shared default. In hindsight, was a JVM-wide shared default a good design, and how should platform teams design defaults in their own libraries?Shutting down executors
Write the standard code to shut down anExecutorService gracefully, and explain what close() added in Java 19 does.During Kubernetes rolling deploys, a Spring Boot service loses in-flight async tasks and some HTTP requests fail. Describe how you would make shutdown graceful end to end.No question matches these choices.