In-depth understanding and principle interpretation of java multithreading



1. Why do we need multiple threads

This question is for people with
a weak foundation; first, let’s mention the QPS calculation formula (concurrency number/response time(s)). The use of multithreading is to increase the number of concurrency, in other words to improve QPS; so many threads Is it true that the more QPS, the higher the performance, the better? Of course not. Although the numerator has increased, the denominator has also increased; the use of multithreading will open up a time slice on the CPU, which will increase the time-consuming context switching; therefore, the more threads are not the better .
Reason 2: If single threading is used in most projects, then from a request to a response, only the protocol analysis and data processing after the response occupies the CPU (not considering computing services), then after the request is sent to the service, the CPU has been idle. State, waiting for the end of IO/disk processing; after the introduction of multi-threading, when the program is processing IO, the CPU can handle other things, and switch back to processing when the current thread needs the CPU; therefore, multi-threading can make full use of the CPU to improve performance ; here Careful friends should also discover a feature of multi-threading . Multi-threading is not really executed concurrently, but different time slices switch back and forth to perform different tasks, but the CPU processing efficiency is very fast, giving people the feeling that they are simultaneously carried out

2. If you use a thread pool, how to set the number of threads in the thread pool

The setting of the number of threads in the thread pool should be introduced in many places. I believe you just read the results and don’t know the principle; I will introduce them in detail here.
First of all , I will mention a few key words, cpu cores, IO-intensive services, Computing-intensive services, blocking coefficient = blocking time/(blocking time + computing time);

blocking here is for IO, so blocking time is IO time; computing time-level CPU time;
then I will give another example If a service has a blocking time of 50 milliseconds and a calculation time of 10 milliseconds, that is, the cpu has 50 milliseconds of idle time; can the cpu handle 5 such requests during this period; plus the first request is a total of processing 6 requests.

So I would propose a formula
threads = ncpu / (1- blockage factor)
and then go into the calculation above 50, 10, if it is a single-core 6 is not that, then you can set about quad-core 24 thread
above formula can also It can be equivalent to: number of threads=ncpu*(1+blocking time/computing time);

when you see here, everyone should understand how to calculate the number of threads, and you may ask questions about why the number of threads for computationally intensive services seen online The formula = ncpu+1 (Ignore the formula for now) is much less than the number of threads set for the IO type service; that is because the calculation type service is called because most of the service itself is calculated, and the calculation is affected CPU time, so a service comes in almost all calculations (CPU) time-consuming, so according to this feature, everyone changes the blocking time above to 0, the calculation time to 50, and the first formula is used. If you find it, it will be 1 core. It is just the number of threads. What is the reason for ncpu+1? Why +1? Bringing it into consideration in reality, computing services cannot be all calculations. There will always be some IO time-consuming. This is undoubtedly. Then change the parameters, IO time-consuming 10, calculation time-consuming 50, then this It corresponds to it; so the number of threads = ncpu/(1-blocking factor) is a general formula ; at the same time, another conclusion is drawn that the larger the blocking factor, the more the number of threads can be set, and vice versa .

3. How is the CPU utilization calculated? How to prevent the CPU from being too high and causing the program to crash

CPU usage rate = CPU calculation time/CPU calculation time + CPU idle time; therefore, if the CPU calculation time is 100 mm and the idle time is 900 mm, then the utilization rate is 10%. If the calculation time is 500 mm and the idle time is 0 mm, then use The rate is 100% (the system counts the value per unit time); therefore, as the CPU continues to calculate, the CPU rate is constantly changing.
If the CPU is too high, you must first locate what is causing it, blindly open new threads, or program calculations are too complex, or unintentional loops or infinite loops; blindly open threads can be solved by thread pool, the latter two are optimizations If the program is not optimized, you can use sleep(0), sleep(1) to solve it; some people may ask what is the meaning of sleep(0), can it optimize the CPU? Of course it can, although it uses the cpu Sleeping for 0 milliseconds, but another function is to trigger the cpu time slice competition (re-election), so there will not be a time slice that monopolizes the entire cpu, causing the cpu to soar instantly;
now that this is the case, I will mention another Question, what is the difference between sleep() and yield()?

  1. After sleep() is executed, all time slices are triggered to be elected together, each time slice may get the right to run the next time, yield() command to give the execution right to the same priority or higher
  2. Thread executes sleep() into blocking state, executes yield() method into ready state
  3. The sleep() statement throws InterruptedException; the yield() method does not declare to throw an exception
  4. sleep() has a time block time parameter; yield() has no parameters (the execution time of the CPU is directly controlled by the JVM)

I typed each word by hand, please give me a thumbs up and support, thank you! If you have different opinions, please comment;

Please indicate the source for reprinting, the original is not easy!

Guess you like

Origin blog.csdn.net/weixin_43225813/article/details/109033843