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.
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%.
So does java 8 automatically distribute the work on all CPU cores? I'm confused...
Here some code...
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:
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() 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 does not automatically distribute the work on all CPU cores, unless your code requests it explicitly (for example by using parallel streams).
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.
Also see these answers:
(Note that I rewrote the answer, removing the part which was corrected by the comments)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With