在CPU上的Java中可以并发运行的最大线程数 [英] Maximum number of threads than can run concurrently in java on a CPU

查看:1166
本文介绍了在CPU上的Java中可以并发运行的最大线程数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对某事感到困惑。
我所知道的是,在现代计算机的普通CPU上可以同时运行的最大线程数范围为8到16个线程。
另一方面,使用GPU可以同时运行数千个线程,而调度程序不会中断任何线程来安排另一个线程。
在以下几个帖子中:
Java虚拟机 - 最大值线程数 https://community.oracle.com/message/10312772
人们说它们在普通的CPU上同时运行数千个java线程。
怎么会这样?
我怎样才能知道可以并发运行的最大线程数,以便我的代码根据底层架构自动调整它。

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 threads https://community.oracle.com/message/10312772 people 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上的任意数量的线程之间来回切换。这就是抢占式多任务处理的含义。

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."

当然,如果线程多于核心,并非所有线程都会同时执行 。有些将被暂停,等待一个时间段。

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.

从性能的角度来看,有用的多少线程正如你所说的那样它取决于可用处理器的数量以及任务是IO还是CPU绑定。尝试找到最佳数字并尽可能使其可配置。

As far as how many threads are useful from 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.

这篇关于在CPU上的Java中可以并发运行的最大线程数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆