在 CPU 上的 Java 中可以并发运行的最大线程数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21650083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:52:29  来源:igfitidea点击:

Maximum number of threads than can run concurrently in java on a CPU

javamultithreadinggpucpu

提问by user3060396

Please I got confused about something. What I know is that the maximum number of threads that can run concurrently on a normal CPU of a modern computer ranges from 8 to 16 threads. On the other hand, using GPUs thousands of threads can run concurrently without the scheduler interrupting any thread to schedule another one. On several posts as: Java virtual machine - maximum number of threadshttps://community.oracle.com/message/10312772people are stating that they run thousands of java threads concurrently on normal CPUs. How could this be ?? And how can I know the maximum number of threads that can run concurrently so that my code adjusts it self dynamically according to the underlying architecture.

请我对某事感到困惑。我所知道的是,现代计算机的普通 CPU 上可以同时运行的最大线程数为 8 到 16 个线程。另一方面,使用 GPU 可以同时运行数千个线程,而无需调度程序中断任何线程来调度另一个线程。在几个帖子中: Java 虚拟机 - 最大线程数https://community.oracle.com/message/10312772人们声称他们在普通 CPU 上同时运行数千个 Java 线程。这怎么可能??以及如何知道可以同时运行的最大线程数,以便我的代码根据底层架构动态调整它。

回答by initramfs

At any given time, a processor will run the number of threads equal to the number of cores contained. This means that on a uniprocessor system, only one thread (or no thread) is being run at any given moment.

在任何给定时间,处理器将运行与包含的内核数相等的线程数。这意味着在单处理器系统上,在任何给定时刻只有一个线程(或没有线程)正在运行。

However, processors do not run each thread one after another, rather they switch between multiple threads rapidly to simulate concurrent execution. If this weren't the case let alone create multiple threads, you won't even be able to start multiple applications.

然而,处理器并不是一个接一个地运行每个线程,而是在多个线程之间快速切换以模拟并发执行。如果不是这种情况,更不用说创建多个线程了,您甚至无法启动多个应用程序。

A java thread (compared to processor instructions) is a very high level abstraction of a set of instructions for the CPU to process. When it gets down to the processor level, there is no guarantee which threads will run on which core at any given time. But given that processors rapidly switch between these threads, it is theoretically possible to create an infinite amount of threads albeit at the cost of performance.

Java 线程(与处理器指令相比)是 CPU 处理的一组指令的非常高级的抽象。当它下降到处理器级别时,无法保证在任何给定时间哪些线程将在哪个内核上运行。但考虑到处理器在这些线程之间快速切换,理论上可以创建无限数量的线程,尽管以性能为代价。

If you think about it, a modern computer has thousands of threads running at the same time (combining all applications) while only having 1 ~ 16 (typical case) number of cores. Without this task-switching, nothing would ever get done.

如果您考虑一下,现代计算机有数千个线程同时运行(结合所有应用程序),而只有 1 到 16 个(典型情况)内核数。如果没有这种任务切换,就什么也做不了。

If you are optimizing your application, you should consider the amount of threads you need by the work at hand, and not by the underlying architecture. Performance gains from parallelism should be weighted against increasing overheads of thread execution. Since every machine is different, every runtime environment is different, it is impractical to work out some golden thread count (however, a ballpark estimate may be made by benchmarking and looking at number of cores).

如果您正在优化您的应用程序,您应该根据手头的工作而不是底层架构来考虑您需要的线程数量。并行性带来的性能提升应该权衡增加的线程执行开销。由于每台机器都不同,每个运行时环境都不同,因此计算出一些黄金线程数是不切实际的(但是,可以通过基准测试和查看内核数量来进行大致估计)。

回答by josefx

There is hardware and software concurrency. The 8 to 16 threads refers to the hardware you have - that is one or more CPUs with hardware to execute 8 to 16 threads parallel to each other. The thousands of threads refers to the number of software threads, the scheduler will have to swap them out so every software thread gets its time slice to run on the hardware.

存在硬件和软件并发。8 到 16 个线程是指您拥有的硬件 - 即一个或多个带有硬件的 CPU,可以并行执行 8 到 16 个线程。数千个线程是指软件线程的数量,调度程序必须将它们换出,以便每个软件线程都有自己的时间片在硬件上运行。

To get the number of hardware threads you can try Runtime.availableProcessors().

要获取硬件线程数,您可以尝试Runtime.availableProcessors().

回答by TypeIA

Threads aren't tied to or limited by the number of available processors/cores. The operating system scheduler can switch back and forth between any number of threads on a single CPU. This is the meaning of "preemptive multitasking."

线程不受可用处理器/内核数量的约束或限制。操作系统调度程序可以在单个 CPU 上的任意数量的线程之间来回切换。这就是“抢占式多任务处理”的含义。

Of course, if you have more threads than cores, not all threads will be executing simultaneously. Some will be on hold, waiting for a time slot.

当然,如果你有超过内核更多的线程,并不是所有的线程将被执行同步。有些将被搁置,等待一个时间段。

In practice, the number of threads you can have is limited by the scheduler - but that number is usually very high (thousands or more). It will vary from OS to OS and with individual versions.

实际上,您可以拥有的线程数受到调度程序的限制 - 但该数量通常非常高(数千或更多)。它会因操作系统和个人版本而异。

As far as how many threads are usefulfrom a performance standpoint, as you said it depends on the number of available processors and on whether the task is IO or CPU bound. Experiment to find the optimal number and make it configurable if possible.

至于从性能的角度来看有多少线程有用,正如您所说,这取决于可用处理器的数量以及任务是 IO 还是 CPU 受限。尝试找到最佳数量,并在可能的情况下使其可配置。

回答by Gaurav Agarwal

While all the other answers have explained how you can theoretically have thousands of threads in your application at the cost of memory and other overheads already well explained here. It is however worth noting that the default concurrencyLevelfor the data structures provided in the java.util.concurrentpackage is 16.

虽然所有其他答案都解释了理论上如何以内存和其他开销为代价在应用程序中拥有数千个线程,但这里已经很好地解释。然而值得注意的是,包中concurrencyLevel提供的数据结构的默认java.util.concurrent值为 16。

You will come across contention issues if you don't account for the same.

如果您不考虑相同的问题,您将遇到争用问题。

Using a significantly higher value than you need can waste space and time, and a significantly lower value can lead to thread contention.

使用明显高于您需要的值会浪费空间和时间,而明显较低的值会导致线程争用。

Make sure you have set the appropriate concurrencyLevelin case you are running into issues related to concurrency with a higher number of threads.

concurrencyLevel如果您遇到与更多线程并发相关的问题,请确保您已设置适当的值。