java - ConcurrentModificationException in a runnable -


i developing timer manager allow multiple countdown timers , cant seem figure out how avoid concurrentmodificationexception. have @ other peoples responses similar problems still cant figure out.

mhandler = new handler();          mupdateui = new runnable() {             public void run() {          iterator<hashmap.entry<string, timerholder>> = mtimers.entryset().iterator();             while (it.hasnext()) {    -----------> map.entry<string, timerholder> pairs = it.next();                 pairs.getvalue().post();             }              iterator<hashmap.entry<string, timerholder>> iterator = mtimers.entryset().iterator();             while (iterator.hasnext()) {                 hashmap.entry<string, timerholder> entry = iterator.next();                 if (!entry.getvalue().isvalid()) {                    iterator.remove();                 }             }                                      mhandler.postdelayed(mupdateui, 1000); // 1 second             }         };         mhandler.post(mupdateui); 

06-02 12:37:23.746: e/androidruntime(10669): fatal exception: main 06-02 12:37:23.746: e/androidruntime(10669): java.util.concurrentmodificationexception 06-02 12:37:23.746: e/androidruntime(10669):    @ java.util.hashmap$hashiterator.nextentry(hashmap.java:806) 06-02 12:37:23.746: e/androidruntime(10669):    @ java.util.hashmap$entryiterator.next(hashmap.java:843) 06-02 12:37:23.746: e/androidruntime(10669):    @ java.util.hashmap$entryiterator.next(hashmap.java:841) 06-02 12:37:23.746: e/androidruntime(10669):    @ com.watcher.timer.timermanager$1.run(timermanager.java:57) 06-02 12:37:23.746: e/androidruntime(10669):    @ android.os.handler.handlecallback(handler.java:730) 06-02 12:37:23.746: e/androidruntime(10669):    @ android.os.handler.dispatchmessage(handler.java:92) 06-02 12:37:23.746: e/androidruntime(10669):    @ android.os.looper.loop(looper.java:137) 06-02 12:37:23.746: e/androidruntime(10669):    @ android.app.activitythread.main(activitythread.java:5493) 06-02 12:37:23.746: e/androidruntime(10669):    @ java.lang.reflect.method.invokenative(native method) 06-02 12:37:23.746: e/androidruntime(10669):    @ java.lang.reflect.method.invoke(method.java:525) 06-02 12:37:23.746: e/androidruntime(10669):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1209) 06-02 12:37:23.746: e/androidruntime(10669):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1025) 06-02 12:37:23.746: e/androidruntime(10669):    @ dalvik.system.nativestart.main(native method) 

actually issue not related concurrency. runnable called same thread - main thread, because post handler.

concurrentmodificationexception thrown because tries modify collection inside for-each loop. that's fail-fast behaviour in order protect non-thread-safe collections potential concurrent modifications. need use iterator explicitly , call remove @ iterator object. "deletes invalid entries" part should this:

iterator<hashmap.entry<string, timerholder>> iterator = mtimers.entryset().iterator(); while (iterator.hasnext()) {     hashmap.entry<string, timerholder> entry = iterator.next();     if (!entry.getvalue().isvalid()) {        iterator.remove();     } } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -