What is the optimal thread pool size for simple java programs to run CPU based tasks

I use thread pool to execute tasks, which is mainly based on a little I / O of CPU,

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

Suppose a simple program submits all tasks to the executor. There is nothing else. I suppose there is a thread pool. Anything that slows down is more likely, because the operating system will have to update time and give each thread in the thread pool a chance to run

Is this correct? If it is a real problem, or most of it is a theoretical problem, that is, if I increase the thread pool size to 1000, I will notice a huge difference

Solution

If you have CPU bound tasks, increasing the number of threads will increase overhead and slow performance Note: having more threads than waiting for a task is a waste of resources, but may not slow the task down

I will use multiple CPUs (such as 1 or 2) instead of adding one, because having too many threads can have amazing overhead

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>