For CPU-intensive tasks, CPU usage is high due to the nature of CPU-intensive tasks. If the number of core threads in the thread pool is too large, context switching times will be increased and additional overhead will be incurred. Therefore, given that cpu-intensive tasks are paused for some reason, additional threads are available to ensure that CPU time is not wasted, and a CPU context switch can be added. In general, the number of core threads in a thread pool is equal to the number of CPU cores +1. For example, there is a lot of computation, video rendering, simulation and so on. At this time, the CPU is running with all its strength. At this time, switching threads wastes the switching time and is not efficient. For example, your brain is the CPU, you are already in a mind to do homework, multithreading this time you write homework, then immediately type code, then P graph, then watch a video, and then switch back to homework. Emmmm, you need to switch (put away your homework, take out your computer, open VS…) I’m afraid you’re going to fail on your homework. This is when you should concentrate on your homework.
For I/ O-intensive tasks, the CPU usage of these tasks is not high. Therefore, the CPU can process other tasks while waiting for I/O operations to make full use of the CPU. In general, the number of core threads is equal to 2 x the number of CPU cores. For example, you need to chat with your sister or brother, download a VS, and read a blog. Let’s say your sister messaged you, responded to her, and then what? It will take time for her to reply to your message, at which point you can search the VS website, download the package, and then look, whoops, she hasn’t replied to you yet, and then look at your own blog. Little sister finally reply to you, you reply to her, then read my blog, this is similar to IO intensive. You can switch between different “brain-free” tasks to be more productive. Rather than the little sister does not reply to my message, I will wait, what all do not do, and so on, the efficiency can be imagined, perhaps, the little sister will not reply to you.
For mixed tasks, the number of threads is related to the thread time because there are two types of tasks. In certain cases, tasks can be divided into I/O intensive tasks and CPU intensive tasks, which can be handled by different thread pools. In general, the number of core threads in the thread pool =(thread wait time/thread CPU time +1)* number of CPU cores;