[OSM-dev] [PATCH] Concurrent modification exception in applet

David Sheldon dave-osm at earth.li
Sun Mar 26 16:50:35 BST 2006


Using the applet recently, it keeps on crashing with a
ConcurrentModificationException in the Java console. This is to do with
the draw() method being called at the same time as a response is
received from the server, causing a change to a line. 

The attached patch creates a copy of the list of lines that will be
iterated over. This is fixed when it starts drawing, and cannot be
modified from external threads so stops the exception problem.

How do you enter new trac tickets? Does it need an account of somesort?

David
-- 
"DecisionSoft - selling coffee to people on the bandwagon."
-------------- next part --------------
Index: src/org/openstreetmap/processing/OsmApplet.java
===================================================================
--- src/org/openstreetmap/processing/OsmApplet.java	(revision 936)
+++ src/org/openstreetmap/processing/OsmApplet.java	(working copy)
@@ -404,11 +404,15 @@
 				}
 			}
 			noFill();
-			
-			// draw the small black border for every line segment
+
+      // Cache as we want to iterate even though another thread
+      // might be editing it.
+      ArrayList allLines = new ArrayList(lines.values());
+
+      // draw the small black border for every line segment
 			strokeWeight(strokeWeight + 2.0f);
 			stroke(0);
-			for (Iterator it = lines.values().iterator(); it.hasNext();) {
+			for (Iterator it = allLines.iterator(); it.hasNext();) {
 				Line line = (Line)it.next();
 				if (line instanceof LineOnlyId)
 					continue;
@@ -422,7 +426,7 @@
 			// draw pending lines (lines that do not belong to a way)
 			strokeWeight(strokeWeight);
 			stroke(255);
-			for (Iterator it = lines.values().iterator(); it.hasNext();) {
+			for (Iterator it = allLines.iterator(); it.hasNext();) {
 				Line line = (Line)it.next();
 				if (line instanceof LineOnlyId)
 					continue;
@@ -451,7 +455,7 @@
 			}
 			
 			boolean gotOne = false;
-			for (Iterator it = lines.values().iterator(); it.hasNext();) {
+			for (Iterator it = allLines.iterator(); it.hasNext();) {
 				Line line = (Line)it.next();
 				if (line instanceof LineOnlyId)
 					continue;
@@ -533,7 +537,7 @@
 			textSize(strokeWeight + 4);
 			textAlign(CENTER);
 
-			for (Iterator e = lines.values().iterator(); e.hasNext();) {
+			for (Iterator e = allLines.iterator(); e.hasNext();) {
 				Line l = (Line)e.next();
 				if (l instanceof LineOnlyId)
 					continue;


More information about the dev mailing list