Skip to content
BytePatterns

Thread Pool Size for a Workload

EasyConcurrency#capacity-math#integer-ceiling~10m

Problem

A service handles requests on a fixed thread pool. Each request computes on the CPU for compute_ms and then waits on the network or a disk for wait_ms, and while a thread waits its core can run another thread. A common sizing rule is threads = cores × target utilization × (1 + wait / compute). Given the core count, the target utilization as a whole percentage, and the two times in milliseconds, return the pool size rounded up. Use integer arithmetic so the answer does not depend on floating-point rounding.

Examples

Input:  cores = 8, utilization = 100, wait_ms = 90, compute_ms = 10
Output: 80
Why:    each core is busy only a tenth of the time, so ten threads share it
Input:  cores = 4, utilization = 50, wait_ms = 30, compute_ms = 20
Output: 5
Why:    4 × 0.5 × 2.5 = 5
Input:  cores = 16, utilization = 100, wait_ms = 0, compute_ms = 5
Output: 16
Why:    edge case, pure CPU work gains nothing from more threads than cores

Hints

0 / 3

Stuck on the idea rather than the code? Sizing a Thread Pool covers it.