java.util.ConcurrentModificationException错误

时间:2016-05-20 21:06:46

标签: java bukkit

我最近遇到了一个错误,当我运行我的runnable更新了矿车的位置时。 Main.UvehicleLocation2HashMap

[22:36:59] [Craft Scheduler Thread - 607/WARN]: Exception in thread "Craft Scheduler Thread - 607" 
[22:36:59] [Craft Scheduler Thread - 607/WARN]: org.apache.commons.lang.UnhandledException: Plugin CreativeControlByKubqoA v2.2.3 generated an exception while executing task 3019245
at org.bukkit.craftbukkit.v1_9_R1.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:56)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1429)
at java.util.HashMap$KeyIterator.next(HashMap.java:1453)
at java.util.AbstractCollection.toArray(AbstractCollection.java:141)
at java.util.ArrayList.<init>(ArrayList.java:177)
at me.kubqoa.creativecontrol.tasks.VehiclesUpdateDB.run(VehiclesUpdateDB.java:19)
at org.bukkit.craftbukkit.v1_9_R1.scheduler.CraftTask.run(CraftTask.java:71)
at org.bukkit.craftbukkit.v1_9_R1.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:53)
... 3 more

这是代码:

@Override
public void run() {
    List<Location> locations1 = new ArrayList<Location>(Main.UvehiclesLocation2.keySet());
    for (Location location : locations1) {
        VehicleHelper.updateVehicle(location,Main.UvehiclesLocation2.get(location));
    }
}

1 个答案:

答案 0 :(得分:0)

在将它复制到新数组列表时,

Main.UvehiclesLocation2.keySet()正在被更改。

您在复制时需要对该实例进行独占访问(例如,使用synchronized阻止),或者您可以将Main.UvehiclesLocation2更改为ConcurrentHashMapFrom the Javadoc

  

视图的迭代器是一个非常一致的&#34;永远不会抛出ConcurrentModificationException的迭代器,并保证在构造迭代器时遍历元素,并且可能(但不保证)反映构造之后的任何修改。