I have a problem. I have to wait for a method I call from a library to finish before I can continue with my code. How can I do that? My code:
Random random = new Random();
int node1 = random.nextInt(graph.getNumberOfVertices() + 1);
int node2 = random.nextInt(graph.getNumberOfVertices() + 1);
MatrixWrappedPath path = graph.getShortestPath(node1, node2);
int pathLength = 0;
if (path != null) {
pathLength = path.getLength();
}
The exception I'm getting from the library ( http://grph.inria.fr/javadoc/index.html ) is:
Exception in thread "main" java.lang.IllegalStateException: cannot compute a distance because the two vertices are not connected
at grph.algo.distance.DistanceMatrix.getDistance(DistanceMatrix.java:56)
at grph.MatrixWrappedPath.getLength(MatrixWrappedPath.java:47)
The DistanceMatrix class runs a BFS (org.dipergrafs.algo.bfs.BFSAlgorithm) which is Multithreaded (org.dipergrafs.algo.SingleSourceSearchAlgorithm, method "compute":
public R[] compute(final Grph g, IntSet sources)
{
final R[] r = createArray(sources.getGreatest() + 1);
new MultiThreadProcessing(g.getVertices(), Grph.getNumberOfThreadsToCreate()) {
@Override
protected void run(int threadID, int source)
{
r[source] = compute(g, source);
}
};
return r;
}
) and fills the DistanceMatrix. So if DistanceMatrix is not finished yet, the getDistance(node1, node2) method cannot get the values from the DistanceMatrix. I read about CountDownLatch
and wait()
, notify()
, but I can't figure out how to do this. What would be a good way to solve this?
You're wrong when assuming there is a multithreading issue there.
The error message is very clear :
(...)cannot compute a distance because the two vertices are not connected
You're taking 2 random nodes in a graph, but these nodes are not connected. that's why the library cannot compute its distance.
Are you sure you didn't forget to add the edges in your graph ? ;)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.