Scrivio Interview library

Executors and thread pools

28 questions in 8 subtopics.

ExecutorService

ConceptFoundations7 min read
What do Executors.newFixedThreadPool, newCachedThreadPool and newSingleThreadExecutor create, and what hidden risk does each carry?
CodingFoundations7 min read
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();
}
Explain and fix, and mention invokeAll and invokeAny.
DebuggingIntermediate7 min read
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;
}
What is wrong and how do you fix it?
LeadershipPrincipal7 min read
With virtual threads final since Java 21, some engineers argue thread pools are obsolete. What role do executors and pools still have, and what would you tell teams to stop doing?

ThreadPoolExecutor parameters

InternalsFoundations6 min read
Explain the order in which ThreadPoolExecutor 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?
ConceptIntermediate7 min read
What do allowCoreThreadTimeOut, prestartAllCoreThreads and keepAliveTime do, and how does Tomcat's executor change the default queue-before-grow behaviour?
DesignIntermediate10 min read
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.
LeadershipAdvanced7 min read
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

ConceptFoundations7 min read
List the built-in ThreadPoolExecutor rejection policies and when each fits.
ScenarioIntermediate7 min read
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.
DesignAdvanced7 min read
Define an overload-handling standard for all services in your platform covering executor rejection, request queues and responses to clients.

Pool sizing

ConceptFoundations7 min read
How do you estimate a thread pool size for CPU-bound work and for I/O-bound work, and what does Runtime.availableProcessors() report in a container?
ScenarioIntermediate7 min read
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?
DesignAdvanced7 min read
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?
LeadershipPrincipal7 min read
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

ConceptFoundations6 min read
What is the difference between scheduleAtFixedRate and scheduleWithFixedDelay, and what happens if a scheduled task throws an exception?
DebuggingFoundations6 min read
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?
ScenarioIntermediate7 min read
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

ConceptFoundations6 min read
What is the difference between ForkJoinPool and a regular ThreadPoolExecutor, and when would you choose each?
InternalsIntermediate7 min read
How does ForkJoinPool work stealing work, and why does it suit divide-and-conquer tasks?
CodingIntermediate7 min read
Write a RecursiveTask<Long> that sums a long[] array, and point out the common mistake in combining subtasks.
ConceptPrincipal7 min read
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

DebuggingFoundations7 min read
A service uses CompletableFuture.supplyAsync(() -> restClient.get(url)) without an executor and parallel streams elsewhere. Under load, both become very slow. Explain.
ScenarioIntermediate7 min read
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?
LeadershipAdvanced6 min read
Several incidents across teams were caused by blocking calls on ForkJoinPool.commonPool via CompletableFuture and parallel streams. What policy and tooling would you introduce?
ConceptPrincipal7 min read
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

CodingFoundations7 min read
Write the standard code to shut down an ExecutorService gracefully, and explain what close() added in Java 19 does.
ScenarioIntermediate7 min read
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.