简体   繁体   English

通过HashMap迭代时出现ConcurrentModificationException

[英]ConcurrentModificationException while iterating through HashMap

I'm getting a ConcurrentModificationException while iterating through a HashMap. 我在通过HashMap进行迭代时收到ConcurrentModificationException

How can I fix this? 我怎样才能解决这个问题?

public void stopAllPlaying(int fadeDurationInMs)
{
    for(PlayThread thread : threadMap.values()) {
        if(thread != null) {
            thread.fadeOut(fadeDurationInMs);
        }
    }
    threadMap.clear();
}

UPDATE: 更新:

Unfortunately using lock doesn't seem to have fixed it. 不幸的是,使用lock似乎并没有解决它。 I've copied the error message below 我已复制以下错误消息

09-06 01:05:51.366: E/AndroidRuntime(1307): FATAL EXCEPTION: main
09-06 01:05:51.366: E/AndroidRuntime(1307): java.util.ConcurrentModificationException
09-06 01:05:51.366: E/AndroidRuntime(1307):     at java.util.HashMap$HashIterator.nextEntry(HashMap.java:796)
09-06 01:05:51.366: E/AndroidRuntime(1307):     at java.util.HashMap$ValueIterator.next(HashMap.java:828)
09-06 01:05:51.366: E/AndroidRuntime(1307):     at com.example.myapp.AudioTrackPlayer.stopAllPlaying(AudioTrackPlayer.java:141)
09-06 01:05:51.366: E/AndroidRuntime(1307):     at com.example.myapp.MyApp.stopAllSounds(MyApp.java:119)
09-06 01:05:51.366: E/AndroidRuntime(1307):     at com.example.myapp.MainActivity.stopPlayback(MainActivity.java:114)
09-06 01:05:51.366: E/AndroidRuntime(1307):     at com.example.myapp.MainActivity.onConfigurationChanged(MainActivity.java:451)
09-06 01:05:51.366: E/AndroidRuntime(1307):     at android.app.ActivityThread.performConfigurationChanged(ActivityThread.java:3397)
09-06 01:05:51.366: E/AndroidRuntime(1307):     at android.app.ActivityThread.handleConfigurationChanged(ActivityThread.java:3542)
09-06 01:05:51.366: E/AndroidRuntime(1307):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1121)
09-06 01:05:51.366: E/AndroidRuntime(1307):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-06 01:05:51.366: E/AndroidRuntime(1307):     at android.os.Looper.loop(Looper.java:150)
09-06 01:05:51.366: E/AndroidRuntime(1307):     at android.app.ActivityThread.main(ActivityThread.java:4263)
09-06 01:05:51.366: E/AndroidRuntime(1307):     at java.lang.reflect.Method.invokeNative(Native Method)
09-06 01:05:51.366: E/AndroidRuntime(1307):     at java.lang.reflect.Method.invoke(Method.java:507)
09-06 01:05:51.366: E/AndroidRuntime(1307):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-06 01:05:51.366: E/AndroidRuntime(1307):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-06 01:05:51.366: E/AndroidRuntime(1307):     at dalvik.system.NativeStart.main(Native Method)

UPDATE 2: 更新2:

I needed to add the lock to all instances where the Hashmap was being modified! 我需要将锁添加到所有修改了Hashmap的实例上!

private final Lock lock = new ReentrantLock();
public void stopAllPlaying(int fadeDurationInMs)
{
    lock.lock();
    try{
      for(PlayThread thread : threadMap.values()) {
          if(thread != null) {
              thread.fadeOut(fadeDurationInMs);
          }
      }
      threadMap.clear();
    }
    finally{
    lock.unlock();
    }
}

This should do the job. 这应该做的工作。

You must lock all other usages of threadMap in the same way with the same Lock instance . 您必须使用相同的Lock实例以相同的方式锁定 threadMap的所有其他用法。

NOTE: This is not the only way to do synchronization. 注意:这不是进行同步的唯一方法。

See also http://developer.android.com/reference/java/util/concurrent/locks/Lock.html 另请参见http://developer.android.com/reference/java/util/concurrent/locks/Lock.html

You may find this helpful, too: http://docs.oracle.com/javase/tutorial/essential/concurrency/sync.html 您也可能会发现这很有帮助: http : //docs.oracle.com/javase/tutorial/essential/concurrency/sync.html

You cannot lock the GUI thread for long as this stalls all application. 您不能锁定GUI线程很长时间,因为这会使所有应用程序停滞不前。 "fadeOut" does not sound as something instant. “ fadeOut”听起来不像是瞬间。

Cascade the two threads: GUI thread calls the intermediate thread and this intermediate thread then calls stopAllPlaying , waiting for the lock if necessary. 级联两个线程:GUI线程调用中间线程,然后该中间线程调用stopAllPlaying ,必要时等待锁定。 The intermediate thread can wait for arbitrary long time. 中间线程可以等待任意长时间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 遍历列表但未修改列表时发生ConcurrentModificationException - ConcurrentModificationException while iterating through a list but not modifying it 迭代Arraylist时的ConcurrentModificationException(不删除) - ConcurrentModificationException while iterating through Arraylist (not removing) 遍历List时发生ConcurrentModificationException,尽管未对其进行任何修改 - ConcurrentModificationException while iterating through List, altough not modifying it 如何在迭代Hashmap时获取ConcurrentModificationException? - How I can get ConcurrentModificationException while iterating Hashmap? 在遍历元素时删除元素。 removeIf 导致 ConcurrentModificationException - Removing element while iterating through it. removeIf result in ConcurrentModificationException Java ConcurrentModificationException:是否可以在迭代时向哈希表添加元素? - Java ConcurrentModificationException: Is it possible to add elements to a hashtable while iterating through it? 在Java中遍历HashMap时出错 - Error while iterating through HashMap in Java 在删除和更新集合时遍历 HashMap - Iterating through a HashMap while removing AND updating the collection 为什么我在同时迭代和修改HashMap时没有得到“ConcurrentModificationException”? - How come I am not getting “ConcurrentModificationException” while iterating and modifying on HashMap at the same time? HashMap ConcurrentModificationException同时初始化另一个HashMap - HashMap ConcurrentModificationException while initializing with another HashMap
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM