如何处理ConcurrentModificationException [英] How to deal with 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);
所以每秒钟都应该把每个玩家的冷却时间减少20个,但问题是我每隔几个小时就会得到一个CME,特别是在很多人在线的时候。我如何做到这一点,如果它仍然在修改列表,它会等到当前的操作完成并创建一个修改队列的种类?谢谢!这里是堆栈跟踪:
2012-06-18 20:59:05 [警告]任务'SurvivorsClasses'生成一个例外
java.util.ConcurrentModificationException $ b $在java.util.HashMap $ HashIterator.nextEntry(HashMap.java:839)$ b $在java.util.HashMap $ KeyIterator.next(HashMap.java: 874)
at me.zachoooo.survivorclasses.CoolDownManager $ CoolDownTimer.run(CoolDownManager.java:13)
at org.bukkit.craftbukkit.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:126)
at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:533)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:459)
解决方案 / div> 使用foreach循环时不能修改集合。
$ b 您可以遍历 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); //通过Map.Entry更新
if(entry.getValue()< = 0){
i.remove(); //通过迭代器移除
code $ pre
I am getting the a ConcurrentModificationException from my cooldown timer. I use a thread to reduce the values every second like this:
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);
}
}
}
}
So every second it should reduce every players cooldown by 20, but the problem is that I a getting the CME every couple hours while running the program, especially when lots of people are online. How do I make it so that if it is still modifying the list, it will wait until the current operation is done and create a sort of modification queue? Thanks! Here is the stack trace:
2012-06-18 20:59:05 [WARNING] Task of 'SurvivorsClasses' generated an exception
java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:839)
at java.util.HashMap$KeyIterator.next(HashMap.java:874)
at me.zachoooo.survivorclasses.CoolDownManager$CoolDownTimer.run(CoolDownManager.java:13)
at org.bukkit.craftbukkit.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:126)
at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:533)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:459)
Line 13 is the start of the for loop...
解决方案 You can't modify collections when using a foreach loop.
You can however iterate over the Map.entrySet()
and do everything you need:
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
}
}
}
这篇关于如何处理ConcurrentModificationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文