Java 从Aparabi线程返回
我正在与Aparabi合作,我无法返回我想要的数据。 我发现Aparabi的工作原理与线程类似Java 从Aparabi线程返回,java,multithreading,aparapi,Java,Multithreading,Aparapi,我正在与Aparabi合作,我无法返回我想要的数据。 我发现Aparabi的工作原理与线程类似 private int calculateBlockLightLevel(final int x,final int y,final int z) { int blockType = world.getBlockTypeAt(x, z, y); //TODO Work out how to edit this, aka not Final! final int fou
private int calculateBlockLightLevel(final int x,final int y,final int z) {
int blockType = world.getBlockTypeAt(x, z, y);
//TODO Work out how to edit this, aka not Final!
final int found[] = new int[]{0};
Kernel kernel = new Kernel(){
@Override public void run() {
int blockLightLevel = getBlockLightLevelAt(x, y + 1, z);
int highestSurroundingBlockLight = blockLightLevel;
found[0]=highestSurroundingBlockLight;
blockLightLevel = getBlockLightLevelAt(x - 1, y, z);
if (blockLightLevel > highestSurroundingBlockLight) {
highestSurroundingBlockLight = blockLightLevel;
found[0]=highestSurroundingBlockLight;
}
blockLightLevel = getBlockLightLevelAt(x + 1, y, z);
if (blockLightLevel > highestSurroundingBlockLight) {
highestSurroundingBlockLight = blockLightLevel;
found[0]=highestSurroundingBlockLight;
}
blockLightLevel = getBlockLightLevelAt(x, y, z - 1);
if (blockLightLevel > highestSurroundingBlockLight) {
highestSurroundingBlockLight = blockLightLevel;
found[0]=highestSurroundingBlockLight;
}
blockLightLevel = getBlockLightLevelAt(x, y, z + 1);
if (blockLightLevel > highestSurroundingBlockLight) {
highestSurroundingBlockLight = blockLightLevel;
found[0]=highestSurroundingBlockLight;
}
blockLightLevel = getBlockLightLevelAt(x, y - 1, z);
if (blockLightLevel > highestSurroundingBlockLight) {
highestSurroundingBlockLight = blockLightLevel;
found[0]=highestSurroundingBlockLight;
}
}
};
kernel.execute(1);
kernel.dispose();
return Math.max(found[0] - Math.max(BLOCK_TRANSPARENCY.get(blockType), 1), 0);
}
从这段代码中,我试图将find返回到我的return中。一旦执行了内核,结果将显示为find。找到的[]数组将传递给GPU,并在执行完成后复制回。把内核想象成另一个匿名的内部类,它可以从封闭的作用域中改变数组内容。Aparabi遵循相同的模式。我忘了添加。仅仅因为数组是最终的,并不意味着它的内容不能更改。找到的[]数组必须是最终数组,匿名类才能在run()方法中“捕获”它。我们不能将find更改为数组引用(不能将find=newint[]{…}!),但可以从内核的run方法中分配给find[]的元素。