What is optimum thread pool size for simple program running cpu based tasks in Java

Paul Taylor picture Paul Taylor · Oct 18, 2012 · Viewed 21.4k times · Source

Im using a thread pool to execute tasks , that are mostly cpu based with a bit of I/O, of size one larger than the number of cpus.

Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() + 1)

Assuming case of simple program that submits all its tasks to this executor and does little else I assume having a thread pool any larger would slow things because the OS would have to timeslice it cpus more often chance to give each thread in the threadpool a chance to run.

Is that correct, and if so is this a real problem or mostly theoretical, i.e if I increased threadpool size to 1000 would I notice a massive difference.

Answer

Peter Lawrey picture Peter Lawrey · Oct 18, 2012

If you have CPU bound tasks, as you increase the number of threads you get increasing overhead and slower performances. Note: having more threads than waiting tasks is just a waste of resources, but may not slow down the tasks so much.

I would use a multiple (e.g. 1 or 2) of the number of cpus rather than adding just one as having one too many threads can have a surprising amount of overhead.