我最近遇到了一个错误,当我运行我的runnable更新了矿车的位置时。 Main.UvehicleLocation2
是HashMap
[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));
}
}
答案 0 :(得分:0)
Main.UvehiclesLocation2.keySet()
正在被更改。
您在复制时需要对该实例进行独占访问(例如,使用synchronized
阻止),或者您可以将Main.UvehiclesLocation2
更改为ConcurrentHashMap
。 From the Javadoc:
视图的迭代器是一个非常一致的&#34;永远不会抛出
ConcurrentModificationException
的迭代器,并保证在构造迭代器时遍历元素,并且可能(但不保证)反映构造之后的任何修改。