Card 08/ 09

ExerciseDifficulty: Advanced1 min

Four Workloads: Choose a Pool and Say Why

Four workloads. Choose what to run each on and say what the wrong choice would cost. All four before the reveal.

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 adds switching and no throughput
2A virtual thread per taskTwenty thousand platform threads is twenty gigabytes of reserved stack
3A single-threaded scheduled executorA new thread per minute, and no way to stop the schedule
4CompletableFuture, combinedThree blocking get() calls in turn take the sum rather than the maximum

The first two are the whole 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, which is what virtual threads made possible.

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.