From 32f61540d7bf4d15f1ae5fa1b8cf56789f4ee207 Mon Sep 17 00:00:00 2001 From: Tuukka Lehtonen Date: Wed, 26 Sep 2018 00:03:39 +0300 Subject: [PATCH] Prevent ConcurrentModificationException in StyledRouteGraphRenderer Fixed via ThreadLocal-based caching of reused data structures. gitlab #132 Change-Id: I42a3d3a160f1f7a9ed30d10cca84e1dc5b6488fa --- .../rendering/StyledRouteGraphRenderer.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/bundles/org.simantics.diagram.connection/src/org/simantics/diagram/connection/rendering/StyledRouteGraphRenderer.java b/bundles/org.simantics.diagram.connection/src/org/simantics/diagram/connection/rendering/StyledRouteGraphRenderer.java index b084e5875..dd9473042 100644 --- a/bundles/org.simantics.diagram.connection/src/org/simantics/diagram/connection/rendering/StyledRouteGraphRenderer.java +++ b/bundles/org.simantics.diagram.connection/src/org/simantics/diagram/connection/rendering/StyledRouteGraphRenderer.java @@ -36,9 +36,13 @@ public class StyledRouteGraphRenderer implements IRouteGraphRenderer, Serializab protected final ConnectionStyle style; + private static class Cache { + Path2D path = new Path2D.Double(); + THashSet branchPoints = new THashSet<>(); + } + // Caches to avoid creating new objects all the time during rendering - protected transient Path2D path; - protected transient THashSet branchPoints; + protected static ThreadLocal caches = ThreadLocal.withInitial(() -> new Cache()); public StyledRouteGraphRenderer(ConnectionStyle style) { if (style == null) @@ -52,14 +56,14 @@ public class StyledRouteGraphRenderer implements IRouteGraphRenderer, Serializab @Override public void render(Graphics2D g, RouteGraph rg) { - if (path == null) - path = new Path2D.Double(); + Cache cache = caches.get(); + Path2D path = cache.path; + THashSet branchPoints = cache.branchPoints; + path.reset(); rg.getPath2D(path); style.drawPath(g, path, false); - if (branchPoints == null) - branchPoints = new THashSet(); branchPoints.clear(); for(RouteLine line : rg.getLines()) { renderLine(g, line, false); -- 2.43.2