Dijkstra在Java中的算法 - 多个请求/线程

时间:2016-10-21 12:47:34

标签: java multithreading web-services executorservice dijkstra

我正在尝试在Java REST webservice中实现Dijkstra的算法,我使用了this链接来帮助我。

在此链接中,它只创建一个图并成功计算一个路径。但是在我的程序中,我创建了多个图形并使用不同的变量作为每个图形的成本。然后我使用ExecutorService(总线程数=图表总数)来并行查找所有路径。当我把它称为测试路径时,我的程序工作正常。

问题是,当该算法同时收到多个请求时,它会在printPath()函数中为某些请求返回“Unreached”消息,并成功返回其他请求的正确路径。我逐个测试了每个路径,并且每次都返回正确的路径而没有任何错误。仅当webservice同时收到多个请求时才会出现问题。下面是我的代码,我只发布了我正在使用的类结构,其余的代码与上面提到的链接相同。

这就是我使用ExecutorService查找路径的方法:

List<RouteFutureResult> rfutureResult = new ArrayList();
    executorService = Executors.newFixedThreadPool(graphs.size());//number of threads is equal to number of graphs
    for (final Graph g : graphs) {
        CalcRoutes calcRoutes = new CalcRoutes(g, other_parameters);
        Future<String> submit = executorService.submit(calcRoutes);
        rfutureResult.add(new RouteFutureResult(submit));
    }
    executorService.shutdown();
    executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.MINUTES);
    //reading response from future object
    List<String> st = new ArrayList();
    for (RouteFutureResult resQuery : rfutureResult) {
        String path = resQuery.getFuture().get();
        st.add(path);
    }

这是我的CalcRoutes.java调用函数:

@Override
public String call() throws Exception {
    List<Double> djst = new ArrayList();
    g.dijkstra(s);
    List<Double> st = g.printPath(d, djst);
    //other processing and returning results
}

这是我在上面提到的链接中使用的Graph类:

    class Graph {
    private final Map<Double, Vertex> graph;
    public class Edge {
        public final double v1, v2;
        public final double dist;
    }
    public class Vertex implements Comparable<Vertex> {
        public final double name;
        double gid;
        public double dist = Double.MAX_VALUE;
        public Vertex previous = null;
        public final Map<Vertex, Double> neighbours = new HashMap<>();
        private void printPath(List<Double> st) {
            if (this == this.previous) {
                st.add(this.name);
            } else if (this.previous == null) {
                System.out.printf("%s(unreached)", this.name);//this is where I am getting a problem when service receives multiple requests at same time
            } else {
                this.previous.printPath(st);
                st.add(this.name);
            }
        }
    }
    public Graph(HashMap<Double, RouteResult> edges) {
    }
    public void dijkstra(double startName) {
        if (!graph.containsKey(startName)) {
            System.err.printf("Graph doesn't contain start vertex \"%s\"\n", startName);
            return;
        }
        final Vertex source = graph.get(startName);
        NavigableSet<Vertex> q = new TreeSet<>();
        for (Vertex v : graph.values()) {
            v.previous = v == source ? source : null;
            v.dist = v == source ? 0 : Double.MAX_VALUE;
            q.add(v);
        }
        dijkstra(q);
    }
    private void dijkstra(final NavigableSet<Vertex> q) {
        Vertex u, v;
        while (!q.isEmpty()) {
        }
    }
    public List<Double> printPath(double endName, List<Double> st) {
        if (!graph.containsKey(endName)) {
            System.err.printf("Graph doesn't contain end vertex \"%s\"\n", endName);
        }
        graph.get(endName).printPath(st);
        return st;
    }
}

这就是我创建多个图表的方式:

Graph cost = new Graph(MRoute); //MRoute is the hashmap
graphs.add(cost);//graphs is a list containing multiple graphs

我还检查了已发布的有关Dijkstra算法的问题,但我找不到与我的问题相关的任何问题。不知何故,这个算法无法处理多个请求,请指导我,任何帮助都将受到高度赞赏。

0 个答案:

没有答案