hasmap的ConcurrentModificationException与列表

时间:2019-07-18 03:17:43

标签: plugins minecraft bukkit java.util.concurrent

我正在尝试解决此问题。我有

ConcurrentHashMap 市场=新的ConcurrentHashMap();

对象包含:库存库存;这是另一个对象。

因此,我试图删除该广告资源拥有的查看者列表。

这是我尝试过的:

for(Iterator<HumanEntity> iter = markets.get(p.getUniqueId()).getInv().getViewers().iterator(); iter.hasNext(); ) {

            HumanEntity student1 = iter.next();
            Player student = (Player) student1;
            if(student.getOpenInventory() != null) {
                student.closeInventory();
                student.sendMessage(Configuration.Msg_Shop_Closed() );
    }

}   

getViewers()返回查看者列表:https://imgur.com/pi1ecD1

这是我得到的错误

org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.EntityLiving.mount(EntityLiving.java:1717) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.EntityPlayer.mount(EntityPlayer.java:595) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.EntityHuman.ak(EntityHuman.java:347) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.World.entityJoinedWorld(World.java:1605) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.World.g(World.java:1582) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.World.entityJoinedWorld(World.java:1652) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.World.g(World.java:1582) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.World.tickEntities(World.java:1425) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.WorldServer.tickEntities(WorldServer.java:597) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:786) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_191]
Caused by: java.util.ConcurrentModificationException
        at java.util.ArrayList$Itr.checkForComodification(Unknown Source) ~[?:1.8.0_191]
        at java.util.ArrayList$Itr.next(Unknown Source) ~[?:1.8.0_191]

1 个答案:

答案 0 :(得分:0)

没关系,我解决了它,对于将来需要这个的你们来说,答案是这样的:


Iterator<Seller> it = markets.values().iterator();

        while(it.hasNext()){    

        List<HumanEntity> buyer  = it.next().getInv().getViewers();

        Iterator<HumanEntity> it2 = buyer.iterator();
        HumanEntity buyer1 = it2.next();
            if( buyer1.getOpenInventory() != null) {
                buyer1.closeInventory();
            }
        }