Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何处理ConcurrentModificationException_Java_For Loop_Runnable_Concurrentmodification - Fatal编程技术网

Java 如何处理ConcurrentModificationException

Java 如何处理ConcurrentModificationException,java,for-loop,runnable,concurrentmodification,Java,For Loop,Runnable,Concurrentmodification,我正在从我的冷却计时器获取ConcurrentModificationException。我使用线程每秒减少一次值,如下所示: public class CoolDownTimer implements Runnable { @Override public void run() { for (String s : playerCooldowns.keySet()) { playerCooldowns.put(s, playerCooldo

我正在从我的冷却计时器获取ConcurrentModificationException。我使用线程每秒减少一次值,如下所示:

public class CoolDownTimer implements Runnable {
    @Override
    public void run() {
        for (String s : playerCooldowns.keySet()) {
            playerCooldowns.put(s, playerCooldowns.get(s) - 20);
            if (playerCooldowns.get(s) <= 0) {
                playerCooldowns.remove(s);
            }
        }
    }

}
第13行是for循环的开始…

当您尝试修改集合的内容时,会在遍历集合的同时抛出ConcurrentModificationException

阅读和了解更多关于它的讨论

有时它可能适合你的原因在文章中已经清楚地提到了

当您尝试修改集合的内容时,会抛出ConcurrentModificationException,同时遍历集合

阅读和了解更多关于它的讨论

有时它可能适合你的原因在文章中已经清楚地提到了


使用foreach循环时不能修改集合

但是,您可以迭代Map.entrySet并执行所需的所有操作:

public void run() {
    for (Iterator<Map.Entry<String,Integer>> i = playerCooldowns.entrySet().iterator(); i.hasNext();) {
        Map.Entry<String,Integer> entry = i.next();
        entry.setValue(entry.getValue() - 20); // update via the Map.Entry
        if (entry.getValue() <= 0) {
            i.remove(); // remove via the iterator
        }
    }
}

使用foreach循环时不能修改集合

但是,您可以迭代Map.entrySet并执行所需的所有操作:

public void run() {
    for (Iterator<Map.Entry<String,Integer>> i = playerCooldowns.entrySet().iterator(); i.hasNext();) {
        Map.Entry<String,Integer> entry = i.next();
        entry.setValue(entry.getValue() - 20); // update via the Map.Entry
        if (entry.getValue() <= 0) {
            i.remove(); // remove via the iterator
        }
    }
}

与数组不同,集合只在编译时检查,而不是在运行时检查,这就是您不能像在循环中放置或删除那样修改集合的原因。

与数组不同,集合只在编译时检查,而不是在运行时检查,这就是您不能像在循环中放置或删除一样修改集合的原因。

您不应该同时修改HashMap,如果您想要并发访问,请尝试使用ConcurrentHashMap而不是HashMap。您不应该同时修改HashMap,如果希望并发访问,请尝试使用ConcurrentHashMap而不是HashMap.i.remove;//通过迭代器删除,是执行tricki.remove的行;//通过迭代器删除,这是执行该技巧的行
public void run() {
    for (Iterator<Map.Entry<String,Integer>> i = playerCooldowns.entrySet().iterator(); i.hasNext();) {
        Map.Entry<String,Integer> entry = i.next();
        entry.setValue(entry.getValue() - 20); // update via the Map.Entry
        if (entry.getValue() <= 0) {
            i.remove(); // remove via the iterator
        }
    }
}