Java 8 自动使用多核?

标签 java multithreading multiprocessing java-8 multicore

我一年前用 java 7 做了一些关于多核的测试。首先我只在主线程中实现了一些计算(CPU 使用率显示只有一个内核完成了所有工作)然后我用一个 ExecutorService 实例实现了 Callable。在运行它时,所有核心都在工作。

现在,一年后,我必须实现一个小程序(使用 java 8)来插入大量数据。所有工作都在主线程中实现(没有 Callable 和 ExecutorService),但是当我运行程序时,CPU 使用率显示所有 4 个内核都为 98%。

那么 java 8 会自动分配所有 CPU 核心上的工作吗?我很困惑...

这里有一些代码...

map 生成器.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);
        }
    }

区域.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() 做一些地理计算,然后读取字节数组中的值,没什么特别的)

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 内核上分配工作,除非您的代码明确请求(例如通过使用并行流)。

在某些特殊情况下,Hotspot 编译器会 auto-vectorize代码,例如参见 JDK-6340864 .但是,自动矢量化使用特殊的 SIMD CPU 指令,而不是多个 CPU。

另请参阅这些答案:

(请注意,我重写了答案,删除了评论更正的部分)

关于Java 8 自动使用多核?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28573180/

相关文章:

c - Linux 中多线程的信号处理

c++ - 为什么 GCC 不使用 LOAD(无围栏)和 STORE+SFENCE 来实现顺序一致性?

multithreading - 连通分量的并行算法

python - python脚本中的多处理函数

java - HashMap get(Key) 函数在 Android AsyncTask 中不起作用

java - for 循环不迭代所有增量

java - "Cannot be resolved to type"错误

java - 最新的 Jersey 示例不起作用

java - 你如何在 java webdriver 中使用线程运行两个 firefox 实例

python - 并行计算-将每个进程输出到文件