Java 8自动使用多核? [英] Java 8 automatically using multicore?
问题描述
我在一年前做了一些关于java7多核的测试。首先我只在主线程中实现了一些计算(CPU使用率表明只有一个核心完成了所有的工作)然后我用ExecutorService实例实现了Callable。在运行它的所有核心工作。
I did some tests a year ago concerning multicore with java 7. First I implemented some calculations only in the main thread (CPU usage showed that only one core did all the work) and then I implemented Callable with an ExecutorService instance. While running it all cores where doing the work.
现在,一年后,我必须实现一个插入大量数据的小程序(使用java 8)。所有工作都在主线程中实现(没有Callable和ExecutorService),但是当我运行程序时,CPU使用情况显示,所有4个内核都是98%。
Now, one year, later I have to implement a little programm (using java 8) which interpolates a lot of data. All the work is implemented in the main thread (without Callable and ExecutorService) but when I'm running the programm the CPU usage shows me, that all 4 cores are at 98%.
java 8是否自动在所有CPU内核上分配工作?我很困惑......
So does java 8 automatically distribute the work on all CPU cores? I'm confused...
这里有一些代码......
Here some code...
MapGenerator.java
MapGenerator.java
Region[][] regions = new Region[numOfRegions][numOfRegions];
for(int x = 0; x < regions.length; x++){
for(int z = 0; z < regions[x].length; z++){
newLat = SRTMHandler.getNewLatitude(startLat, z * regionSize * 16);
newLon = SRTMHandler.getNewLongitude(startLon, x * regionSize * 16, newLat);
regions[x][z] = new Region(x, z, regionSize, newLat, newLon);
}
}
Region.java:
Region.java:
private Chunk[] chunks;
public Region(int x, int z, int size, float startLat, float startLon){
this.chunks = new Chunk[this.size][this.size];
//Init stuff
float newLat = this.startLat, newLon = this.startLon;
for(int newX = 0; newX < this.size; newX++){
for(int newZ = 0; newZ < this.size; newZ++){
newLat = SRTMHandler.getNewLatitude(this.startLat, newZ * 16);
newLon = SRTMHandler.getNewLongitude(this.startLon, newX * 16, newLat);
this.chunks[newX][newZ] = new Chunk(this.x * this.size + newX, this.z * this.size + newZ, 16, 900, this, newLat, newLon);
}
}
}
Chunk.java:(SRTMHandler .getHeightForLatLon()执行一些地理计算,然后读取字节数组中的值,没什么特别的)
Chunk.java: (SRTMHandler.getHeightForLatLon() does some geo calculations and then reads a value in a byte array, nothing special)
public Chunk(int x, int z, int size, int height, Region r, float startLat, float startLon){
this.blocks = new Block[size][size][height];
//Init stuff
try {
this.calcSurface();
//System.out.println("Finished " + this.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void calcSurface() throws IOException{
int x1 = this.x;
int x2 = this.x + 16;
int z1 = this.z;
int z2 = this.z + 16;
final int radius = 45;
float q11 = SRTMHandler.getHeightForLatLon(SRTMHandler.getNewLatitude(this.startLat, (-1)*radius), SRTMHandler.getNewLongitude(this.startLon, (-1)*radius, this.startLat));
float q12 = SRTMHandler.getHeightForLatLon(SRTMHandler.getNewLatitude(this.startLat, radius), SRTMHandler.getNewLongitude(this.startLon, (-1)*radius, this.startLat));
float q21 = SRTMHandler.getHeightForLatLon(SRTMHandler.getNewLatitude(this.startLat, (-1)*radius), SRTMHandler.getNewLongitude(this.startLon, radius, this.startLon));
float q22 = SRTMHandler.getHeightForLatLon(SRTMHandler.getNewLatitude(this.startLat, radius), SRTMHandler.getNewLongitude(this.startLon, radius, this.startLat));
for(int x = 0; x < this.blocks.length; x++){
for(int z = 0; z < this.blocks[x].length; z++){
float height = Interpolation.biLerp(x, z, q11, q12, q21, q22, x1, x2, z1, z2);
this.blocks[x][z][(int)Math.round(height)] = new Block(this.x * this.size + x, this.z * this.size + z, (int)Math.round(height), BlockType.Grass, this);
}
}
}
推荐答案
Java 8不会自动在所有CPU内核上分配工作,除非您的代码明确地请求它(例如通过使用并行流)。
Java 8 does not automatically distribute the work on all CPU cores, unless your code requests it explicitly (for example by using parallel streams).
在某些特殊情况下,Hotspot编译器将 auto -vectorize 代码,例如参见 JDK-6340864 。但是,自动矢量化使用特殊的 SIMD CPU指令,而不是多个CPU。
In some special cases the Hotspot compiler will auto-vectorize the code, see for example JDK-6340864. However, automatic vectorization is using special SIMD CPU instructions, not multiple CPUs.
另请参阅以下答案:
- Does the JVM have the ability to detect opportunities for parallelization?
- Automatic parallelization
(请注意,我重写了答案,删除了由评论更正的部分)
(Note that I rewrote the answer, removing the part which was corrected by the comments)
这篇关于Java 8自动使用多核?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!