Situation
I have a visualisation using JGraph. The graph is updated by a different thread to that which instantiates the Visualisation.
Expected Behaviour
The graph should be updated by various worker threads. The function that the threads call to update the graph is syncronized
, so the worker threads will not causes concurrency issues between themselves.
Actual Behaviour
There is (ocassionally) an exception thrown in the AWT-EventQueue thread when painting. Sometimes it's a null pointer, sometimes it's an index out of bounds. Here's a stack trace:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at com.mxgraph.shape.mxConnectorShape.translatePoint(Unknown Source) at com.mxgraph.shape.mxConnectorShape.paintShape(Unknown Source) at com.mxgraph.canvas.mxGraphics2DCanvas.drawCell(Unknown Source) at com.mxgraph.view.mxGraph.drawState(Unknown Source) at com.mxgraph.swing.mxGraphComponent$mxGraphControl.drawCell(Unknown Source) at com.mxgraph.swing.mxGraphComponent$mxGraphControl.drawChildren(Unknown Source) at com.mxgraph.swing.mxGraphComponent$mxGraphControl.drawCell(Unknown Source) at com.mxgraph.swing.mxGraphComponent$mxGraphControl.drawChildren(Unknown Source) at com.mxgraph.swing.mxGraphComponent$mxGraphControl.drawCell(Unknown Source) at com.mxgraph.swing.mxGraphComponent$mxGraphControl.drawChildren(Unknown Source) at com.mxgraph.swing.mxGraphComponent$mxGraphControl.drawCell(Unknown Source) at com.mxgraph.swing.mxGraphComponent$mxGraphControl.drawFromRootCell(Unknown Source) at com.mxgraph.swing.mxGraphComponent$mxGraphControl.drawGraph(Unknown Source) at com.mxgraph.swing.mxGraphComponent$mxGraphControl.paintComponent(Unknown Source) at javax.swing.JComponent.paint(JComponent.java:1029) at com.mxgraph.swing.mxGraphComponent$mxGraphControl.paint(Unknown Source) at javax.swing.JComponent.paintChildren(JComponent.java:866) at javax.swing.JComponent.paint(JComponent.java:1038) at javax.swing.JViewport.paint(JViewport.java:764) at javax.swing.JComponent.paintToOffscreen(JComponent.java:5138) at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:302) at javax.swing.RepaintManager.paint(RepaintManager.java:1188) at javax.swing.JComponent._paintImmediately(JComponent.java:5086) at javax.swing.JComponent.paintImmediately(JComponent.java:4896) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:783) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:735) at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677) at javax.swing.RepaintManager.access$700(RepaintManager.java:58) at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1593) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:226) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:647) at java.awt.EventQueue.access$000(EventQueue.java:96) at java.awt.EventQueue$1.run(EventQueue.java:608) at java.awt.EventQueue$1.run(EventQueue.java:606) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105) at java.awt.EventQueue.dispatchEvent(EventQueue.java:617) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177) at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)
It doesn't seem to adversely affect the running of the application. A new AWT-EventQueue spawns, which will at some stage inevitably encounter the same problem.
Cause
I think it must caused by updating the graph in a separate thread to the one that the JFrame is instantiated in. The things that need to be drawn are being changed whilst the paint method is trying to draw them.
Question
How do I go about working around this problem? Can I somehow synchronize the paint method with the method that updates the graph?
Visualisation Code
package ui;
import javax.swing.JFrame;
import com.mxgraph.layout.mxOrganicLayout;
import com.mxgraph.layout.mxStackLayout;
import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxGraphModel;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;
import core.Container;
import core.Node;
import core.WarehouseGraph;
public class Visualisation extends JFrame{
private static final long serialVersionUID = 8356615097419123193L;
private mxGraph graph = new mxGraph();
Object parent = graph.getDefaultParent();
mxOrganicLayout graphlayout = new mxOrganicLayout(graph);
mxStackLayout containerLayout = new mxStackLayout(graph, true, 10);
public Visualisation(WarehouseGraph model){
super("Warehouse Simulator");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800, 600);
graph.getModel().beginUpdate();
try{
for(Node node : model.getNodes().values()){
mxCell cell = (mxCell)graph.insertVertex(parent, node.getId(), node.getId(), 0, 0, 60, 30);
Object prev = null;
for(Container container : node.getContainers()){
Object newCont = graph.insertVertex(cell, container.getId(), container.getId(), 0, 0, 60, 20);
if(prev != null) graph.insertEdge(cell, null, null, prev, newCont);
prev = newCont;
}
}
for(Node node : model.getNodes().values()){
for(Node toNode : node.getDownstreamNodes()){
Object fromCell = ((mxGraphModel)graph.getModel()).getCell(node.getId());
Object toCell = ((mxGraphModel)graph.getModel()).getCell(toNode.getId());
graph.insertEdge(parent, null, null, fromCell, toCell);
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
graph.getModel().endUpdate();
}
graphlayout.execute(parent);
Object[] nodes = mxGraphModel.getChildVertices(graph.getModel(), parent);
for(Object cell : nodes){
containerLayout.execute(cell);
graph.updateCellSize(cell);
}
mxGraphComponent graphComponent = new mxGraphComponent(graph);
getContentPane().add(graphComponent);
this.setVisible(true);
}
//CALLED FROM SYNCHRONIZED FUNCTION
public void moveContainer(Container container, Node to){
graph.getModel().beginUpdate();
mxCell toCell = (mxCell)((mxGraphModel)graph.getModel()).getCell(to.getId());
mxCell containerCell = (mxCell)((mxGraphModel)graph.getModel()).getCell(container.getId());
mxCell fromCell = (mxCell)containerCell.getParent();
try{
Object[] edges = mxGraphModel.getEdges(graph.getModel(), containerCell);
graph.removeCells(edges);
containerCell.removeFromParent();
graph.addCell(containerCell, toCell);
Object[] containers = mxGraphModel.getChildVertices(graph.getModel(), toCell);
if(containers.length >= 2)
graph.insertEdge(toCell, null, null, containerCell, containers[containers.length-2]);
containerLayout.execute(toCell);
containerLayout.execute(fromCell);
}catch(Exception e){
e.printStackTrace();
}finally{
graph.getModel().endUpdate();
}
}
}
Controller Code
package core;
import serialisation.JsonUtil;
import ui.Visualisation;
public class Controller{
private WarehouseGraph graph;
Visualisation viz;
public static void main(String[] args){
Controller.getInstance();
}
private Controller(){
graph = JsonUtil.initFromJson(); //graph has many worker threads running which can call containerMoved
viz = new Visualisation(graph);
}
private static class SingletonHolder{
private static final Controller INSTANCE = new Controller();
}
public static Controller getInstance(){
return SingletonHolder.INSTANCE;
}
public synchronized void containerMoved(Container container, Node from, Node to){
viz.moveContainer(container, to);
}
}