gpt4 book ai didi

java - 可运行中的 ConcurrentModificationException

转载 作者:行者123 更新时间:2023-12-01 18:31:24 25 4
gpt4 key购买 nike

我正在开发一个计时器管理器,它将允许多个倒计时器,但我似乎无法弄清楚如何避免此 ConcurrentModificationException。我看过其他人对类似问题的 react ,但仍然无法弄清楚。

mHandler = new Handler();

mUpdateUI = new Runnable() {
public void run() {

Iterator<HashMap.Entry<String, TimerHolder>> it = 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): at java.util.HashMap$HashIterator.nextEntry(HashMap.java:806)
06-02 12:37:23.746: E/AndroidRuntime(10669): at java.util.HashMap$EntryIterator.next(HashMap.java:843)
06-02 12:37:23.746: E/AndroidRuntime(10669): at java.util.HashMap$EntryIterator.next(HashMap.java:841)
06-02 12:37:23.746: E/AndroidRuntime(10669): at com.watcher.timer.TimerManager$1.run(TimerManager.java:57)
06-02 12:37:23.746: E/AndroidRuntime(10669): at android.os.Handler.handleCallback(Handler.java:730)
06-02 12:37:23.746: E/AndroidRuntime(10669): at android.os.Handler.dispatchMessage(Handler.java:92)
06-02 12:37:23.746: E/AndroidRuntime(10669): at android.os.Looper.loop(Looper.java:137)
06-02 12:37:23.746: E/AndroidRuntime(10669): at android.app.ActivityThread.main(ActivityThread.java:5493)
06-02 12:37:23.746: E/AndroidRuntime(10669): at java.lang.reflect.Method.invokeNative(Native Method)
06-02 12:37:23.746: E/AndroidRuntime(10669): at java.lang.reflect.Method.invoke(Method.java:525)
06-02 12:37:23.746: E/AndroidRuntime(10669): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
06-02 12:37:23.746: E/AndroidRuntime(10669): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
06-02 12:37:23.746: E/AndroidRuntime(10669): at dalvik.system.NativeStart.main(Native Method)

最佳答案

实际上您的问题与并发无关。您的 Runnable 始终从同一个线程 - Main 线程调用,因为您将其发布到 Handler

ConcurrentModificationException 被抛出,因为您尝试在 for-each 循环内修改集合。这是快速失败行为,目的是保护非线程安全集合免受潜在的并发修改的影响。您需要显式使用 Iterator 并在迭代器对象上调用 remove 。您的“删除无效条目”部分应如下所示:

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();
}
}

关于java - 可运行中的 ConcurrentModificationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24000285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com