我有一个返回一组点的方法,我确信这个方法的 for 循环部分可以拆分为一个 RecursiveTask,它为每个线程返回一组点。
我已经尝试了很多次,但都失败了。那里有Java天才吗?
我现有的方法:
private Set<Point3D> getCordinatesAroundCenterPoint(Point3D point, int width) {
Set<Point3D> points = new LinkedHashSet<>();
double maxValue = width;
double minValue = maxValue * -1;
double minX = point.getX() + minValue;
double maxX = point.getX() + maxValue;
double minY = point.getY() + minValue;
double maxY = point.getY() + maxValue;
double minZ = point.getY() + minValue;
double maxZ = point.getZ() + maxValue;
double x = point.getX();
double y = point.getY();
double z = point.getZ();
double numberOfPoints = Math.pow((double) (maxValue * 2) + 1, Double.parseDouble("3"));
for (int i = 1; i <= numberOfPoints; i++) {
if (x > maxX) {
x = minX;
y++;
}
if (y > maxY) {
y = minY;
z++;
}
if (z > maxZ) {
z = minZ;
}
Point3D ppoint = new Point3D();
ppoint.setX(x);
ppoint.setY(y);
ppoint.setZ(z);
points.add(ppoint);
x++;
}
return points;
}
更新#1:
这是我尝试将其拆分为递归任务的尝试,它似乎在范围为 1(应该等于 27 点)-1、0、+1、= 3 点、3 立方 = 27 的情况下工作正常。任何更高的数字范围失败,例如范围为 2 应返回 125 点。-2、-1、0、+1、+2 = 5 分,5 立方 = 125。
主.java:
public static void main(String[] args) {
System.out.println("Enter system extent: ");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
try {
String string = bufferedReader.readLine();
LinkedHashSet<Point3D> set = getStarSystemCordinatesAroundCenterSystem(Integer.parseInt(string));
System.out.println(set.size() + " systems generated.");
} catch (IOException e) {
e.printStackTrace();
}
}
private static LinkedHashSet<Point3D> getStarSystemCordinatesAroundCenterSystem(int extent) {
ForkJoinPool pool = new ForkJoinPool();
double maxValue = extent;
double minValue = maxValue * -1;
ForkPoints task = new ForkPoints(minValue, maxValue);
LinkedHashSet<Point3D> linkedHashSet = (LinkedHashSet<Point3D>) pool.invoke(task);
return linkedHashSet;
}
ForkPoints.java:
public class ForkPoints extends RecursiveTask<Set<Point3D>> {
private static final long serialVersionUID = -5450450150370659468L;
private double minValue;
private double maxValue;
static final double SEQUENTIAL_THRESHHOLD = 2;
public ForkPoints(double minValue, double maxValue) {
this.minValue = minValue;
this.maxValue = maxValue;
}
@Override
protected Set<Point3D> compute() {
if (maxValue - minValue <= SEQUENTIAL_THRESHHOLD) {
return computeValue(minValue, maxValue);
} else {
double midValue = minValue + SEQUENTIAL_THRESHHOLD;
ForkPoints left = new ForkPoints(minValue, midValue);
ForkPoints right = new ForkPoints(midValue, maxValue);
left.fork();
Set<Point3D> rightPoints = right.compute();
Set<Point3D> leftPoints = left.join();
leftPoints.addAll(rightPoints);
return leftPoints;
}
}
private Set<Point3D> computeValue(double minv, double maxv) {
//Assume starting point of 0,0,0
double minX = 0 + minv;
double maxX = 0 + maxv;
double minY = 0 + minv;
double maxY = 0 + maxv;
double minZ = 0 + minv;
double maxZ = 0 + maxv;
double x = minv;
double y = minv;
double z = minv;
Set<Point3D> points = new LinkedHashSet<>();
boolean notFinished = true;
while (notFinished) {
if (x > maxX) {
x = minX;
y++;
}
if (y > maxY) {
y = minY;
z++;
}
if (z > maxZ) {
z = minZ;
}
Point3D ppoint = new Point3D();
ppoint.setX(x);
ppoint.setY(y);
ppoint.setZ(z);
points.add(ppoint);
if (x == maxX && y == maxY && z == maxZ) {
notFinished = false;
}
x++;
}
return points;
}
}