简体   繁体   English

Java是否具有自动并行化功能?

[英]Does Java have automatic parallelization?

I wrote a computationally heavy single threaded program, but when I executed it I was surprised to see that my quad core processor was at 95% utilization. 我编写了一个计算量大的单线程程序,但是执行该程序时,我惊讶地发现我的四核处理器利用率为95%。 I was expecting to see something close to 25% utilization (one core). 我期望看到利用率接近25%(一个内核)。 I used the Process Explorer from sysinternals and was further surprised to see that the Java process had six threads with almost identical load distribution of about 16% total processor time. 我使用了来自sysinternalsProcess Explorer ,并且惊讶地发现Java进程有六个线程,它们的负载分配几乎相同,约占总处理器时间的16%。

Initially I was planning to parallelize my code, but now it seems that I won't gain any performance since the CPU is already at 95% load even with my non-parallel code. 最初,我打算对代码进行并行化,但是现在看来,即使使用非并行代码,CPU的负载也已达到95%,所以我不会获得任何性能。

It almost appears that Java is somehow automatically parallelizing my code, but everything on the net tells me that this cannot be true. Java似乎以某种方式自动地使我的代码并行化,但是网上的所有内容都告诉我这不可能成立。 Does anyone know what's going on? 有人知道发生了什么吗?

Edit: I added some screenshots to hopefully answer some of the questions below: 编辑:我添加了一些屏幕截图,希望可以回答以下一些问题:

System at idle - 1% load. 系统处于空闲状态-负载为1%。 I am uisng a quad core CPU with 8 logical cores. 我正在使用具有8个逻辑内核的四核CPU。 闲置系统

My non-parallel java program running. 我的非并行java程序正在运行。 Note that all 4 cores are nearly maxed out: 请注意,所有4个内核几乎都已用尽: 跑

View of my non-parallel java program from SysInternals Process Explorer. 从SysInternals Process Explorer中查看非并行Java程序的视图。 Note the 8 threads with nearly identical load: 注意8个线程几乎具有相同的负载: procexp

On most systems I've used, usage percentages are actually what percent of one core's power the process is using. 在我使用的大多数系统上,使用百分比实际上是该进程使用的一个内核功率的百分比。 If your system is like that, you're simply misinterpreting the usage percentage. 如果您的系统是这样的,您只是在误解使用百分比。 That is to say, java is using 95% of one CPU. 也就是说,java正在使用一个 CPU的95%。 Not sure if that is what is going on, but it would explain how an unparallelized program could display at 95%. 不知道这是怎么回事,但是它将解释无并行程序如何以95%的比例显示。

EDIT: Based on your edits, this does not appear to be the case. 编辑:根据您的编辑,情况似乎并非如此。 Perhaps like @a_horse_with_no_name suggests in the comments, your code is using a parallel stream or another automatically parallelized operation? 也许就像@a_horse_with_no_name在注释中建议的那样,您的代码正在使用并行流还是其他自动并行化的操作?

Java is not parallelizing your code. Java不会并行化您的代码。 Java is parallelizing itself. Java正在使自身并行化。 Remember Java runs in a virtual machine. 请记住,Java在虚拟机中运行。 That virtual machine is a program in itself. 该虚拟机本身就是一个程序。

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

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