01.合适的线程数量
最优线程数计算理论依据
The minimum number of threads is equal to the number of available
cores. If all tasks are computation intensive, then this is all we need. Having
more threads will actually hurt in this case because cores would be context
switching between threads when there is still work to do. If tasks are IO
intensive, then we should have more threads.
We can compute the total number of threads we’d need as follows:
Number of threads = Number of Available Cores / (1 - Blocking Coefficient)
To determine the number of threads, we need to know two things:
• The number of available cores
• The blocking coefficient of tasks
The first one is easy to determine; we can look up that information, even at
runtime, as we saw earlier. It takes a bit of effort to determine the blocking
coefficient. We can try to guess it, or we can use profiling tools or the java.
lang.management API to determine the amount of time a thread spends on
system/IO operations vs. on CPU-intensive tasks.网友的一点补充
参考
Last updated