知识库--Concurrency+Determining the Number of Threads(76)

Determining the Number of Threads
//线程数量是计算出来的 绝不是猜测出来的,dubbo的超时时间,重试次数亦如此

For a large problem, we’d want to have at least as many threads as the number of available cores. This will ensure that as many cores as available to the process are put to work to solve our problem. We can easily find the number of available cores; all we need is a simple call from the code:

    Runtime.getRuntime().availableProcessors();

So, 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.

When a task performs an IO operation, its thread gets blocked. The processor immediately context switches to run other eligible threads. If we had only as many threads as the number of available cores, even though we have tasks to perform, they can’t run because we not scheduled them on threads for the processors to pick up.//线程是cpu的调度单位

If tasks spend 50 percent of the time being blocked, then the number of threads should be twice the number of available cores. If they spend less time being blocked—that is, they’re computation intensive—then we should have fewer threads but no less than the number of cores. If they spend more time being blocked—that is, they’re IO intensive—then we should have more threads, specifically, several multiples of the number of cores.

We can compute the total number of threads we’d need as follows:

    Number of threads = Number of Available Cores / (1 - Blocking Coefficient)
    where the blocking coefficient is between 0 and 1.//堵塞系数

A computation-intensive task has a blocking coefficient of 0, whereas an IO-intensive task has a value close to 1–a fully blocked task is doomed, so we don’t have to worry about the value reaching 1.

Note: the blocking coefficient

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.

你可能感兴趣的:(并发)