简体   繁体   English

JVM中有多个线程-它们何时使用多个内核?

[英]Multiple threads in JVM - when are they using multiple cores?

I recently had a chance to listen to another discussion about the JVM. 最近,我有机会听了关于JVM的另一个讨论。 It was stated that Java Virtual Machine is well built in terms of concurrency. 据说Java虚拟机在并发方面构建良好。 I could not find any satisfying answer to what I thought to be a simple question: when JVM runs multiple-threads (and therefore uses multiple virtual-cores, right?) does it make use of multiple real cores of machine's CPU? 对于一个我认为简单的问题,我找不到任何令人满意的答案:当JVM运行多线程(并因此使用多个虚拟内核,对吗?),它是否利用了计算机CPU的多个真实内核?

I heard "not always" or "sometimes" as an answer; 我听到“并非总是”或“有时”作为答案; so is there any way to ensure that when we design our code to run multiple threads the JVM will use multiple cores of the CPU as well? 那么有什么方法可以确保当我们设计代码以运行多个线程时,JVM也将使用CPU的多个核心吗? Or the other way, what determines whether the JVM uses mutliple CPU cores or not? 或以其他方式,由什么决定JVM是否使用多核CPU内核?

I am not really able to give an example of when this would be necessary, but I find it interesting, as I know designers who prefer everything to be deterministic in their project. 我真的无法给出何时需要这样做的示例,但是我发现它很有趣,因为我知道设计师更喜欢项目中所有具有确定性的东西。 And what would really be the point of having multiple threads in some big applications if for real they would never be computed parallely? 如果真正不能对它们进行并行计算,那么在某些大型应用程序中拥有多个线程的真正意义是什么?

Java threads, like regular threads, may be scheduled to use multiple cores. Java线程与常规线程一样, 可以安排使用多个内核。 The real sticky thing about concurrent programming is that it's hard to "force" a program to use X number of cores and to have this thread run on this core, etc. In other words, to be deterministic. 关于并发编程的真正棘手的事情是,很难“强制”程序使用X个内核,并使该线程在该内核上运行,等等。换句话说,是确定性的。

It's ultimately up to the OS and in some sense the hardware. 最终取决于操作系统以及某种程度上的硬件。 But at the end of the day the programmer should be thinking about concurrent programming abstractly and trusting that the scheduler is doing its job. 但最终,程序员应该抽象地考虑并发编程,并相信调度程序正在完成其工作。

In other words, yes. 换句话说,是的。

在所有主流的现代jvm中,java线程都是“真正的”操作系统线程,因此,就像任何其他OS线程一样,它们将跨内核调度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM