简体   繁体   English

单线程程序,以利用多个核心

[英]Single thread program to make use of multiple cores

Java程序的单个线程是否可以自动使用CPU上的多个内核?

Can a single thread of a Java program automatically make use of multiple cores on the CPU? Java程序的单个线程是否可以自动使用CPU上的多个内核?

Yes and no. 是的,不是。 A single threaded Java program will use multiple threads in that the GC, JMX, finalizer, and other background threads can run in different CPUs (whether CPU or core). 单线程Java程序将使用多个线程,因为GC,JMX,终结器和其他后台线程可以在不同的CPU(无论是CPU还是内核)中运行。 The GC threads especially give a significant performance boost if they can be running in another CPU. 如果GC线程可以在另一个CPU中运行,那么它们可以显着提高性能。 However, your single threaded application code, although it may move between CPUs, will never be running in 2 CPUs at the same time. 但是,您的单线程应用程序代码虽然可能在CPU之间移动 ,但永远不会同时在2个CPU中运行。

how to find out that? 如何找出答案?

That's a harder question and it depends on what architecture you are running on. 这是一个更难的问题,它取决于您正在运行的架构。 ps under *nix will be able to show if you have multiple threads in the run queue but even then it may not show that they are actually executing in multiple CPUs. * nix下的ps将能够显示运行队列中是否有多个线程,但即使这样,它也可能无法显示它们实际上是在多个CPU中执行。

Your own code will not run on multiple cores if it is by definition single threaded. 如果按照定义单线程,您自己的代码将无法在多个核心上运行。 No single threaded application can run simultaneously on multiple cores - unless your using underluing multithreaded calls/libraries without knowing. 没有单个线程应用程序可以在多个内核上同时运行 - 除非您在不知情的情况下使用欠载多线程调用/库。

Usually gc is running in a separate thread. 通常gc在一个单独的线程中运行。 But usually it doesn't make any significant difference. 但通常它没有任何显着差异。 That's all. 就这样。

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

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