[英]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.