Card 08/ 09
All 9 cards
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 workload | |
|---|---|
| 1 | Resizing ten thousand images; each takes a second of pure computation |
| 2 | Twenty thousand open connections, each mostly waiting on the network |
| 3 | One task every minute, forever, checking a directory |
| 4 | Three independent service calls whose results are combined into one response |
The four choices, and what the wrong one costs
| Use | The wrong choice costs | |
|---|---|---|
| 1 | A fixed pool, sized to the processor count | More threads than cores adds switching and no throughput |
| 2 | A virtual thread per task | Twenty thousand platform threads is twenty gigabytes of reserved stack |
| 3 | A single-threaded scheduled executor | A new thread per minute, and no way to stop the schedule |
| 4 | CompletableFuture, combined | Three 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.