Reprinted: 79885842
Directory
- First, throw the problem
- Two. Analysis
- Third, practical application
- Iv. Summary:
Body
Go back to top one, throw a question
about how to calculate the number of concurrent threads, generally divided into two factions, from two books, and are good books, in the end which is right? After the problem is traced, organize the following:
The first faction: Java Concurrency in practice, Java concurrency Programming practice, such as:
For example, in the Java Concurrency in practice book, the formula for estimating the size of the thread pool is given:
nthreads=ncpu*ucpu* (1+W/C), where
NCPU=CPU Number of cores
UCPU=CPU usage rate, 0~1
W/c= ratio of wait time to calculation time
Second faction: Programming Concurrency on the JVM mastering "Java Virtual machine Concurrent Programming"
Number of threads =ncpu/(1-blocking factor)
Back to top two. Analysis
For faction one, assume that the cpu100% is running, that is, the number of threads =ncpu* (1+W/C) Aside from the CPU utilization factor.
Now suppose that the faction two formula equals faction one formula, namely ncpu/(1-blocking factor) =ncpu* (1+w/c), = = = "Blocking factor =w/(W+C), that is, blocking factor = blocking time/(blocking time + calculation time), this conclusion is enlisted in the follow-up of Faction II, such as:
Thus, faction one and faction two are actually a formula ... So I'm relieved ...
Back to top third, practical application
So how do you set the number of concurrent threads in actual use? The analysis is as follows (we take the faction formula as an example):
nthreads=ncpu* (1+W/C)
IO-intensive: In general, if there is Io, then certainly w/c>1 (blocking time is generally a lot of time to calculate times), but need to consider the system memory is limited (each open a thread requires memory space), here need to test the number of servers on the server is appropriate (CPU ratio, number of threads, Total time, memory consumption). If you do not want to test, conservative point to take 1 that is, nthreads=ncpu* (=2NCPU). This setting is generally OK.
Compute-intensive: w/c=0 If there is no waiting for w=0. Nthreads=ncpu.
The conclusion is:
IO-intensive =2NCPU (can be tested after you control the size, 2Ncpu generally no problem) (often appear in the thread: Database data interaction, file upload download, network data transfer, etc.)
Compute-intensive =ncpu (often appearing in threads: Complex algorithms)
In Java: ncpu=Runtime.getRuntime().availableProcessors()
========================= here can be skipped =============================================
Of course the faction of a "Java Concurrency in Practice" also has a saying,
For computationally intensive tasks, on systems with N processors, when the thread pool size is n+1, the optimal efficiency is usually achieved. This extra thread ensures that the CPU's clock cycles are not wasted, even when compute-intensive threads occasionally pause for missing or other reasons. )
That is, compute-intensive =ncpu+1, but this practice causes more than one CPU context switch to be worthwhile, which is not considered here. Readers can consider their own.
======================================================================
Back to the top four, summary:
There are many factors to choose the number of concurrent threads in the thread pool: The task type, memory, and all resources used in threads need to be considered. Through the analysis and demonstration of the existing literature, this paper draws a conclusion, and gives the practical application formula, which is the welfare of the engineer and the model of the technology.
Determine the number of thread pool concurrent threads based on the number of CPU cores