Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 673
Paul Taylor Avatar asked Oct 18 '12 09:10

Paul Taylor


People also ask

What is the ideal thread pool size in Java?

4 cores = 400 / second (or 800 maybe with HT). The blocking coefficent is 90ms/10ms = 9. So the ideal thread pool size is 4 cores * 2 * ( 1 + 9 ) = 80.

How do you optimize a thread pool in Java?

You can create this type of thread pool by using the newScheduledThreadPool() factory method: ScheduledThreadPoolExecutor executor = (ScheduledThreadPoolExecutor) Executors. newScheduledThreadPool(5); This creates a thread pool with a corePoolSize of 5, an unbounded maximumPoolSize and a keepAliveTime of 0 seconds.

What is pool size in thread?

The size of a thread pool is the number of threads kept in reserve for executing tasks. It is usually a tunable parameter of the application, adjusted to optimize program performance. Deciding the optimal thread pool size is crucial to optimize performance.


2 Answers

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.

like image 63
Peter Lawrey Avatar answered Sep 28 '22 01:09

Peter Lawrey


For reference, check this description.

http://codeidol.com/java/java-concurrency/Applying-Thread-Pools/Sizing-Thread-Pools/

In short, what you have (No. CPU + 1) is optimal on average.

like image 35
Alan J Liu Avatar answered Sep 28 '22 01:09

Alan J Liu