【问题标题】:Swing Worker - Functions being called in their own orderSwing Worker - 按自己的顺序调用的函数
【发布时间】:2016-09-06 07:03:25
【问题描述】:

我正在尝试确保 JFrame 在更新 GUI 时保持响应。我为此使用 SwingWorker。该场景是在程序的顺序流期间执行以下代码。

public int[][] readInputFile(MatrixOperations matrixOperations, 
        String inputFile, Boolean isDirected) throws IOException {
    List<String> fileLines = Files.readAllLines(Paths.get(inputFile),
            StandardCharsets.UTF_8);        
    int[][] adjacencyMatrix = matrixOperations.createAdjacencyMatrixFromFile(
                            fileLines, isDirected);


        //The following two lines perform the computationally expensive operation.
        //So I'm asking SwingWorker to do this.
        task = new Task(fileLines, isDirected, ROOT_GRAPH);
        task.execute();


    return adjacencyMatrix;

}

这是正在执行的 SwingWorker 类。我面临的问题是在 doInBackground() 中执行 for 循环期间。在 doInBackground() 的执行过程中,createNodeFromFile(source)、createNodeFromFile(destination)、createEdgeFromFile(source、destination、isDirected) 这三个函数按自己的顺序调用,但它们应该按顺序调用。结果,我得到了 ConcurrentModification 异常,或者有时 Node 不存在异常(这是 Graphstream 库特定的异常)。我该如何解决这个问题?

import java.util.List;

import javax.swing.SwingWorker;

import org.graphstream.graph.Graph;

public class Task  extends SwingWorker<Void, Integer>{

List<String> list;
Boolean isDirected;
Graph graph;

public Task(List<String> list, Boolean isDirected, Graph graph){
    this.list = list;
    this.isDirected = isDirected;
    this.graph = graph;
}
@Override
public Void doInBackground() throws Exception {
    // TODO Auto-generated method stub
    int count = 0;
    for (int i = 0; i < list.size(); i++) {
        // System.out.println(fileContent.get(i));
        if ((!list.get(i).startsWith("#"))
                && (list.get(i).length() > 1)) {
            count++;
            String[] arrLine = list.get(i).trim().split("\\s+");
            String source = arrLine[0].trim();
            String destination = arrLine[1].trim();

            createNodeFromFile(source);
            createNodeFromFile(destination);
            createEdgeFromFile(source, destination, isDirected);
        }
        if (count % 1000 == 0) {
            System.out.println("Finished processing " + count + " lines");
        }
    }
    return null;
}



public void createNodeFromFile(String nodeId) {
    if (graph.getNode(nodeId) == null) {
        graph.addNode(nodeId);
        showLabelOnRootGraph(nodeId, nodeId);

    }
}

public void showLabelOnRootGraph(String nodeId, String label) {
     graph.getNode(nodeId).addAttribute("ui.label", label);
}

public void createEdgeFromFile(String source, String destination, Boolean isDirected) {
    if (graph.getNode(source).hasEdgeBetween(destination) == false) {
        graph.addEdge("S" + source + "D" + destination, source,
                destination, isDirected);
    }
}

}

【问题讨论】:

    标签: java multithreading swingworker


    【解决方案1】:

    我找到了答案。基本上,我们必须从 Swing Worker 返回对象 Graph。然后必须将该对象传递给 process()。这是解决方案:

    package main.graph.generate;
    
    import java.util.Iterator;
    import java.util.List;
    
    import javax.swing.SwingWorker;
    
    import org.graphstream.graph.Graph;
    import org.graphstream.ui.view.Viewer;
    import org.graphstream.ui.view.ViewerPipe;
    
    public class Task  extends SwingWorker<Graph, Graph>{
    
    List<String> list;
    Boolean isDirected;
    Graph graph;
    ViewerPipe viewerPipe;
    
    public Task(List<String> list, Boolean isDirected, Graph graph, ViewerPipe viewerPipe){
        this.list = list;
        this.isDirected = isDirected;
        this.graph = graph;
        this.viewerPipe = viewerPipe;
    }
    
    @Override
    public Graph doInBackground() throws Exception {
        // TODO Auto-generated method stub
        int count = 0;
        //for (int i = 0; i < list.size(); i++) {
            // System.out.println(fileContent.get(i));
        for(Iterator it = list.iterator();it.hasNext();){
            String line = (String) it.next();
            if ((!line.startsWith("#"))
                    && (line.length() > 1)) {
                count++;
                String[] arrLine = line.trim().split("\\s+");
                String source = arrLine[0].trim();
                String destination = arrLine[1].trim();
    
    
                createNodeFromFile(source);
                createNodeFromFile(destination);
                createEdgeFromFile(source, destination, isDirected);
                publish(graph);
            }
            if (count % 1000 == 0) {
                System.out.println("Finished processing " + count + " lines");
            }
        }
    
    
    
    
    
        return graph;
    }
    
    @Override
    protected void process(List<Graph> graphs){     
        GraphGenerate.ROOT_GRAPH = graphs.get(graphs.size()-1);
        viewerPipe.addAttributeSink(GraphGenerate.ROOT_GRAPH);
        }
    
    public void createNodeFromFile(String nodeId) {
        if (graph.getNode(nodeId) == null) {
            graph.addNode(nodeId);
            showLabelOnRootGraph(nodeId, nodeId);
    
        }
    }
    
    public void showLabelOnRootGraph(String nodeId, String label) {
         graph.getNode(nodeId).addAttribute("ui.label", label);
    }
    
    public void createEdgeFromFile(String source, String destination, Boolean isDirected) {
        if (graph.getNode(source).hasEdgeBetween(destination) == false) {
            graph.addEdge("S" + source + "D" + destination, source,
                    destination, isDirected);
        }
    }
    
    }
    

    干杯!

    【讨论】:

      猜你喜欢
      • 2013-05-28
      • 2012-01-07
      • 2020-07-21
      • 1970-01-01
      • 2018-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多