Card 08/ 09

ExerciseDifficulty: Advanced1 min

Practice: Choosing an Executor for Four Workloads

Computing, waiting, scheduling and combining: which pool fits each, and what the wrong one costs.

Work that computes wants about as many threads as there are processor cores; work that waits wants as many threads as there is work, which virtual threads make affordable. For each workload below, choose what to run it on and say what the wrong choice would cost, all four before you open the answer.

The four workloads
The workload
1Resizing ten thousand images; each takes a second of pure computation
2Twenty thousand open connections, each mostly waiting on the network
3One task every minute, forever, checking a directory
4Three independent service calls whose results are combined into one response
The four choices, and what the wrong one costs
Each workload, the choice, and what it avoids
UseThe wrong choice costs
1A fixed pool, sized to the processor countMore threads than cores add switching and no throughput
2A virtual thread (a lightweight thread the JVM schedules itself) per taskTwenty thousand platform threads reserve around twenty gigabytes of stack
3A single-threaded scheduled executorA new thread per minute, and no way to stop the schedule
4CompletableFuture (a future you can chain and combine), combinedThree blocking get() calls in turn take the sum of the times, not the maximum

The first two are most of pool sizing. Work that computes wants about as many threads as there are cores; work that waits wants as many as there is work.

The fourth is the one that looks fine in review. Submitting all three and then collecting takes as long as the slowest; submitting and waiting one at a time takes as long as all three together.