Java – number of processor cores and size of thread pool

Many times, I've heard that the number of threads maintained in the thread pool is better than the number of cores in the system Having two or more threads than the number of cores is not only a waste, but also may lead to performance degradation

Is it true? If not, what is the fundamental principle of depriving these statements (specifically related to Java)?

Solution

The declaration is an incorrect general declaration That is, sometimes they are real (or real), and other times they are hypocritical

Several things are indisputably true:

>More threads means more memory usage Each thread needs a thread stack For the most recent hotspot JVM, the minimum thread stack size is 64KB, and the default value can be as high as 1MB This may be important In addition, any living thread may own or share an object in the heap, whether it is currently runnable or not Therefore, it is reasonable to expect that more threads mean a larger memory working set. > The JVM cannot actually run more threads by executing the hardware core (or hyper threading kernel or any other kernel) A car will not run without an engine, and a thread will not run without a core

Beyond that, things became less clear A "problem" is an active thread that can be in various "states" For example:

Active threads can run; That is, actively execute instructions Active threads can run; That is, wait for a core so that it can run Active threads can be synchronized; That is, waiting for a signal from another thread or waiting for the lock to be released The active thread can wait for external events; For example, wait for some external server / service to respond to the request

The "one thread per core" heuristic assumes that threads are running or runnable (according to the above) But for many multithreaded applications, the heuristic is wrong... Because it doesn't consider threads in other states

Now "too many" threads can obviously lead to significant performance degradation, simply by using too much memory (imagine that you have 4GB of physical memory and create 8000 threads with 1MB stack, which is a way to subvert virtual memory.)

But what about other things? Too many threads will lead to too many context switches?

I don't think so. If you have many threads and your application uses them, it may lead to too many context switches, which is bad for performance However, I don't think the root cause of context switching is the actual number of threads The root cause of performance problems is more likely to be applications:

>Synchronize in a particularly wasteful manner; For example, when object Notify () is better for using object Notifyall() > synchronize or on highly competitive data structures > do too much synchronization relative to the useful workload each thread is doing > try to do too much I / O parallelism

(in the last case, the bottleneck may be the I / O system rather than context switching... Unless the I / O is an IPC with services / programs on the same machine)

On the other hand, multithreading does not increase context switching in the absence of confusion If your application has n runnable threads competing for M processors, and the threads are purely computational and non competitive, the operating system's thread scheduler will try to time slice between them However, the time slice length may be measured in a tenth of a second (or more), so the context switching overhead is negligible compared with the work actually performed by the CPU bound thread in its slice If we assume that the length of time slice is constant, the context switching overhead will also be constant Adding more runnable threads (increasing N) does not significantly change the ratio of work to overhead

In short, "too many threads" is harmful to performance However, in fact, there is no universal "rule of thumb" for how many "too many" Fortunately, you usually have considerable leeway before performance problems become significant

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>