First, ask questions

There are generally two schools of thought on how to count concurrent threads, two books, both good books, which one is right? After tracing the problems, we sorted them out as follows:

The first class, Java Concurrency in Practice, is the Java Concurrency Practice.

See below, in Java Concurrency in Practice, a formula for estimating the thread pool size:

Nthreads = Ucpu Ncpu * * (1 + w/c), one of them

Ncpu = number of CPU core

Ucpu= CPU usage, 0 to 1

W/C= ratio of waiting time to calculation time

Programming Concurrency on the JVM Mastering

Number of threads =Ncpu/ (1- blocking factor)

Analysis of two.

For faction 1, assume that the CPU runs 100%, that is, the number of threads =Ncpu*(1+w/c), regardless of CPU usage.

Now assume that the formula of faction 2 is equal to the formula of faction 1, that is, Ncpu/ (1-blocking coefficient) =Ncpu*(1+ W /c),=== “blocking coefficient = W /(w+ C), that is, blocking coefficient = blocking time /(blocking time + calculation time). This conclusion is applied in the follow-up of Faction 2, as shown in the figure below:

So faction one and faction two are the same formula… That’s a relief…

Three, practical application

So how to set the actual number of concurrent threads in use? The analysis is as follows (we take the faction-one formula as an example) :

Nthreads=Ncpu*(1+ W/C), where W is the blocking time, C is the computation time, and Ncpu is the number of CPU cores (number of available processors).

IO intensive: usually, if there is IO, so sure w/c > 1 (blocking time are generally time consuming a lot of times), but need to consider the system memory is limited (each open a thread requires memory space), there needs to be on server test how many threads for specific (CPU accounted for, the number of threads, the total time and memory consumption). Nthreads=Ncpu*(1+1)=2Ncpu This setup is usually OK.

Computationally intensive: Assuming no wait w=0, then W/C= 0.nThreads =Ncpu.

So the conclusion is:

IO intensive =2Ncpu (you can control the size after testing, 2Ncpu is generally ok) (common in threads: database data interaction, file upload and download, network data transfer, etc.)

Computation-intensive =Ncpu (common in threads: complex algorithms)

In Java: Ncpu =Runtime.getRuntime().availableProcessors()

end