Note that there are some explanatory texts on larger screens.

plurals
  1. POConcurrentModificationException on synchronized method
    text
    copied!<p>I'm processing a lot of events that come from a TCP socket (sets of ten per second), so I'm using multithreading to process those events.</p> <pre><code>public class MainActivity extends Activity { ... // In this Map I store the tab name and the associated TabHost.TabSpec instance private static Map&lt;String, TabHost.TabSpec&gt; Tabs = Collections.synchronizedMap(new LinkedHashMap&lt;String, TabHost.TabSpec&gt;()); // In this Map I store pairs of tab-names and a HashSet of undelivered messages // It's a class that extends Map&lt;String, HashSet&lt;String&gt;&gt; with some additional functions that doesn't have anything to do with Tabs. private static NamesListing Names = Collections.synchronizedMap(new LinkedHashMap&lt;String, HashSet&lt;String&gt;&gt;()); // Yes, I know the names don't follow the Java standards, but I keeped them for mantaining the question coherence. I will change it in my code, though. synchronized private static void ProcessEvent(final String name, final String message) { // Low-priority thread final Thread lowp = new Thread( new Runnable() { public void run() { final Iterator&lt;String&gt; iter = Tabs.keySet().iterator(); while (iter.hasNext()) { final String tabname = iter.next(); // This just returns an int making some calculations over the tabname final int Status = Names.getUserStatus(tabname, message); // Same than getUserStatus if ((Names.isUserSpecial(Status)) &amp;&amp; (name.equals(tabname))) { // This just removes a line from the HashSet Names.delLine(tabname, message); } } } }); lowp.setPriority(3); lowp.start(); } ... } </code></pre> <p>Most of time this works right, but sometimes there are some avalanches of those events, and somethimes I get a ConcurrentModificationException:</p> <blockquote> <p>12-10 14:08:42.071: E/AndroidRuntime(28135): FATAL EXCEPTION: Thread-369 12-10 14:08:42.071: E/AndroidRuntime(28135): java.util.ConcurrentModificationException 12-10 14:08:42.071: E/AndroidRuntime(28135): at java.util.LinkedHashMap$LinkedHashIterator.nextEntry(LinkedHashMap.java:347) 12-10 14:08:42.071: E/AndroidRuntime(28135): at java.util.LinkedHashMap$KeyIterator.next(LinkedHashMap.java:367) 12-10 14:08:42.071: E/AndroidRuntime(28135): at es.irchispano.chat.MainActivity$6.run(MainActivity.java:244) 12-10 14:08:42.071: E/AndroidRuntime(28135): at java.lang.Thread.run(Thread.java:841)</p> </blockquote> <p>Note: The 244 line corresponds to the</p> <pre><code>final String tabname = iter.next(); </code></pre> <p>statement.</p> <p>It seems weird to me as I'm using a Collections.synchronizedMap and the method that processes those lines is synchronized, so why is it still happening?</p> <p>Thanks!</p> <p><strong>---------- Edited ----------</strong></p> <p>Sorry for the concise initial code; I've tried to simplify as much as possible but obviously it was not a good idea. I'm pasting the actual code. Of course each of those structures are initialized (otherwise I wouldn't have a problem :-)), now I'm going to read all your comments with conscience and I'll post what I'll find out. Thank you all for the support!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload