我正在尝试实现一个 Fork Join Pool,它将带一个节点的子节点并同时探索它们。但是我认为我的 fork 连接池执行线程然后关闭太快,导致线程停止执行?
到目前为止,我有这个代码:
主要方法:
while (!(pqOpen.IsEmpty()))
{
tempVertex = pqOpen.GetNextItem();
if (tempVertex.city == endLocation.city)
{
resetVertex();
return tempVertex;
}
else
{
ForkJoinPool forkJoinPool = new ForkJoinPool(tempVertex.neighbors.GetNoOfItems());
for (int i = 0; i < tempVertex.neighbors.GetNoOfItems(); i++) //for each neighbor of tempVertex
{
forkJoinPool.execute(new NeighbourThread(tempVertex, allVertices, routetype, pqOpen, i, endLocation));
}
forkJoinPool.shutdown();
}
}
return null;
}
这是我运行的课程:
public class NeighbourThread extends RecursiveAction {
Vertex tempVertex, endLocation;
VertexHashMap allVertices;
int routetype, i;
PriorityQueue pqOpen;
public NeighbourThread(Vertex tempVertex, VertexHashMap allVertices, int routetype, PriorityQueue pqOpen, int i, Vertex endLocation)
{
this.allVertices = allVertices;
this.routetype = routetype;
this.tempVertex = tempVertex;
this.pqOpen = pqOpen;
this.i = i;
this.endLocation = endLocation;
}
@Override
public void compute() {
Edge currentRoad = tempVertex.neighbors.GetItem(i);
Vertex vertexNeighbour = allVertices.GetValue(currentRoad.toid);
if (vertexNeighbour.inClosed)//
return null;
if ((!vertexNeighbour.inOpen) || temp_g_score < vertexNeighbour.getTentativeDistance())
{
vertexNeighbour.from = tempVertex;
vertexNeighbour.setTentativeDistance(temp_g_score);
// if neighbor isn't in open set, add it to open set
if (!vertexNeighbour.inOpen)
{
vertexNeighbour.inOpen = true;
pqOpen.AddItem(vertexNeighbour);
}
}
}
我已经删除了 compute() 中的大部分代码,因为我认为它与问题无关。
我认为问题在于 forkJoinPool.shutdown() 行在已创建的线程完成执行之前被执行。有什么方法可以确保线程在我循环回到 while 循环的顶部之前完成?