Thread Pool in Application ProgramResource allocation and processing have been optimized. If you only need to simply open several more threads to process data at the same time, and the execution time of each thread is very short, using a thread pool is a good choice.
However, if the execution time of each sub-thread is long and the total amount of data to be processed is huge, the main thread is continuously starting new threads, I personally feel that the thread pool is not a good choice.
Me
I have had such an experience. If I feel that the thread pool is used in this case, on the one hand, the speed seems to be limited,
Although the maximum number of threads in the thread pool can be changed, it is always not allowed to increase freely (of course, within the acceptable range of hardware) and cannot reach the desired speed; on the other hand, I always doubt whether it is a waiting thread.
When there are too many threads, the thread pool will automatically cut down some threads, resulting in data loss (of course, just suspect ).
Therefore, I used to control the number of threads and set a parameter as the upper limit of the total number of threads. When the total number of threads in the current process of the program exceeds the set value, wait for a while before starting the new thread. In this case, the speed is up, and no data loss is found.
The above is my personal opinion. It is for your reference only. Do you have any new ideas to share with us! I will continue to pay attention to this content in practice ~