The number of threads, the number of shooting more comfortable?

I believe we have used the thread pool, but the pool is set to the number of threads much more reasonable it?

The primary purpose of setting the number of threads in order to fully and rational use of CPU and memory resources, thus maximizing the performance, so let's go explore it!

First, take into account the number of CPU cores, then how to obtain a core number of threads in Java?

May be used Runtime.getRuntime().availableProcessor()a method to obtain (may not be accurate, as a reference)

After confirming the number of cores, go judgment is CPU-intensive tasks or IO-intensive tasks:

  • CPU-intensive tasks: such as encryption, compression, and a series requires a lot of computing tasks consume CPU resources, most scenes are pure CPU computing .
  • IO-intensive tasks: such as the MySQL database, reading and writing files, network communications and other tasks, these tasks will not be particularly consume CPU resources, but the IO operation is time-consuming, will take more time .

Knowing how to determine the task categories, let's discuss two scenarios:

CPU-intensive tasks

For CPU-intensive computing, multi-threaded nature is to enhance the utilization of multi-core CPU, so for an 8-core CPU, a core thread each, in theory, create eight threads on it.

If too many threads, not actually play very good results. At this time, suppose we set the number of threads is 2 times the number of CPU cores, since very heavy computing tasks, it will take a lot of CPU resources, so when each CPU core work are basically at full capacity, and we have set up too many threads, each thread want to use CPU resources to perform their tasks, which would cause unnecessary context switching, this time increasing the number of threads and did not let the performance, but due to the number of threads can cause excessive performance degradation.

Thus, for CPU-intensive computing scenarios, theoretically the number of threads = CPU core number is the most appropriate, but usually the number of threads is set to the number of +1 CPU core , will achieve optimal utilization. Even when intensive memory page thread due to the occasional failure or other causes when blocked, the additional threads can ensure CPU cycles are not wasted, so as to ensure the utilization of the CPU.

Following FIG 8 is a core CPU in the computer, to test the effect on CPU-intensive tasks (prime number calculation) performance by modifying the number of threads.

Can be seen that when the number of threads less than 8, the performance is poor, the number of threads than the number of processor cores to enhance performance is small, it is possible to verify the applicability of a certain formula or.

In addition, we may also want to consider what other simultaneously consume excessive CPU resources that the program is running on the same machine, then do the whole balance of resource use.

IO-intensive tasks

The maximum number of threads for IO-intensive tasks typically many times greater than the number of CPU cores, because in terms of speed IO read and write speeds compared to the CPU is relatively slow, if we set the number of threads is too small, it may cause the CPU resources waste. And if we set up a few more threads, then when a portion of the thread is waiting for IO, and they do not need at this time to calculate the CPU, so they can use another thread CPU to perform other tasks, independently of each other, so waiting in the job queue tasks will be reduced, better use of resources.

For IO intensive computing scenarios, the optimal number of threads is time-consuming and CPU computing and IO program operations related to the following ratio, author "Java Concurrency in combat," the Brain Goetz recommended calculation methods:

线程数 = CPU 核心数 * (1 + IO 耗时/ CPU 耗时)

By this formula, we can calculate a reasonable number of threads, if the average waiting time is long task, it increases the number of threads, and if the average long working hours, which is above our CPU-intensive tasks on the number of threads reduced. Statistical methods to be employed for each APM time-consuming tool, and to facilitate the calculation IO consuming CPU time consuming.

The entire Java Concurrency in Figure combat, to facilitate easier to understand:

Three thread execution schematic

There is a school of calculated "Java Virtual Machine concurrent programming" proposed:

线程数 = CPU 核心数 / (1 - 阻塞系数)

Wherein the blockage factor is computationally intensive 0, IO intensive blockage factor close to 1, generally considered to be between 0.8 and 0.9. For example 8-core CPU, according to the formula is 2 / (1 - 0.9) = the number of threads 20

The figure is a test IO intensive tasks, a different number of threads is open on a dual core processor (from 1-40) to test the effect on the performance of the program, the number of threads can be seen after the pool reached 20, the level curve gradually, no amount of explanation open thread performance improvement program has not helped.

The number of threads too little will make the program overall performance degradation, while too many threads will consume other resources such as memory, so if you want a more accurate, it can be measured pressure, as well as monitoring the thread of the CPU load JVM, according to actual measure of the number of threads should be created, rational and full use of resources.

At the same time, there are a lot of thread pool of applications, such as Tomcat, Redis, Jdbc, etc., the number of threads per application settings are different, such as Tomcat flow inlet, set the number of threads may have to be larger than other applications.

to sum up

By exploring the number of threads on the set, we can see that set the number of threads and cores CPU First of great relevance, in addition, we need to select the corresponding strategy depending on the type of task, the average working time for a thread of proportion the higher, the less you need to thread; the higher the thread of the proportion of the average waiting time, we need more threads; for different programs, corresponding to the actual test you can get the most appropriate choice.

reference

"Java Concurrency in combat."

"Java Virtual Machine concurrent programming."

Java Concurrency combat

Java Concurrency core

Guess you like

Origin www.cnblogs.com/wupeixuan/p/12563640.html