Sub topic 9
Choosing an Executor
At the intermediate level, this chapter assumes you know Java 8 lambdas, CompletableFuture, ExecutorService, and the difference between CPU-bound work and blocking work such as file or network I/O.
An executor is an object that chooses where asynchronous tasks run. CompletableFuture uses an executor for methods such as supplyAsync and thenApplyAsync. Choosing the executor affects throughput, latency, and whether unrelated tasks can interfere with one another.
The common pool
If you do not provide an executor, an asynchronous method usually uses the JVM's common pool:
CompletableFuture<String> result =
CompletableFuture.supplyAsync(() -> "hello");The common pool is a shared ForkJoinPool. It is convenient for short, CPU-bound tasks. Several parts of an application may use the same pool, so tasks compete for its worker threads.
The common pool is a poor default for operations that block for an unpredictable time. A task that waits for a database, HTTP response, or file can occupy a worker while other tasks wait for an available worker.
Supplying a custom executor
Pass an Executor as the final argument to an asynchronous method:
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CustomExecutorDemo {
public static void main(String[] args) {
ExecutorService ioPool = Executors.newFixedThreadPool(8);
try {
CompletableFuture<String> response =
CompletableFuture
.supplyAsync(() -> loadFromService(), ioPool)
.thenApplyAsync(String::toUpperCase, ioPool);
System.out.println(response.join());
} finally {
ioPool.shutdown();
}
}
private static String loadFromService() {
return "data";
}
}Here, both asynchronous stages use ioPool. The try/finally block shuts down the executor so its threads do not keep the application alive after main finishes.
A fixed-size pool gives you a simple concurrency limit. If eight tasks are blocked in loadFromService, later tasks wait in the executor's queue. That limit can protect a downstream service, but it can also increase latency if the pool is too small.
Choose a pool based on the work:
| Work type | Suitable executor | Main concern |
|---|---|---|
| Short CPU calculations | Common pool or bounded CPU pool | Avoid too many competing threads |
| Blocking network or database calls | Dedicated, bounded I/O pool | Limit downstream pressure |
| Latency-sensitive subsystem | Dedicated executor | Prevent unrelated work from delaying it |
Keep independent pools independent
A dedicated executor isolates one kind of work from another. For example, an HTTP client pool can be separate from a CPU-heavy parsing pool. This prevents blocked HTTP tasks from consuming threads needed for parsing.
The isolation is useful, but every pool adds threads, queues, and shutdown responsibilities. Create a custom pool when you need control over capacity, isolation, or thread configuration. Do not create a new pool for every individual task.
Common mistakes
A custom executor does not make blocking work non-blocking. It only moves the blocking to threads that you control.
Also, passing an executor to one stage does not automatically configure every later stage. Pass it explicitly to each *Async stage that must use it:
CompletableFuture<String> result = first
.thenApplyAsync(this::parse, cpuPool)
.thenComposeAsync(this::fetchDetails, ioPool);The first stage uses cpuPool; the dependent fetch uses ioPool. Use this arrangement only when the separation reflects real work boundaries. Moving every stage between pools can add scheduling overhead and make failures harder to diagnose.
Exercise
Why might a database call use a dedicated executor instead of the common pool?
Show answer
The database call can block a thread. A dedicated, bounded executor isolates that waiting and limits how many calls can pressure the database or affect unrelated common-pool tasks.