简体   繁体   English

在两个线程中使用相同的Hashmap时,Hashmap中的并发修改异常

[英]Concurrent Modification Exception in Hashmap while using the same Hashmap in two threads

I am developing a multi-player snake game played over the network. 我正在开发通过网络玩的多人蛇游戏。 It is running fine however it occasionally throws java.util.ConcurrentModification Exception. 它运行良好,但是偶尔会抛出java.util.ConcurrentModification Exception。 This is thrown in the main class of my game in the paintComponent() method. 这是在我的游戏的主类中使用paintComponent()方法抛出的。 Code at which this is thrown is 引发此错误的代码是

 for (String name : map.keySet()) {
        if (!map.get(name).gameover) {
            for (int i = 0; i < map.get(name).length; i++) {
                rect = new Rectangle2D.Double(map.get(name).p[i].x,
                        map.get(name).p[i].y, width, width);
                g1.setColor(Color.black);
                g1.draw(rect);
                g1.setPaint(map.get(name).snakecolor);
                g1.fill(rect);
            }
        }
    }

The Hashmap map is a mapping from Hashmap映射是来自

        HashMap<String,Snake> 

where Snake is the class which has all the attributes of a snake. 其中Snake是具有蛇的所有属性的类。 The main class also runs a thread side by side for receiving messages and updates from other clients through the server. 主类还并行运行一个线程,以通过服务器从其他客户端接收消息和更新。

The iterator on the thread side also uses the same map(passed by reference to that class). 线程侧的迭代器也使用相同的映射(通过对该类的引用传递)。 Code for that is as given below. 如下所示。 This function is called if the score of any player reaches some specific point after which the level is upgraded. 如果任何玩家的分数达到某个特定点,然后升级级别,则调用此功能。

    void levelUp(int level){
    for(String name:map.keySet()){
        map.get(name).level=level;
    }
    Game.speed=100/level;
}

I think the clash between object write is due to this. 我认为对象写入之间的冲突是由于这个原因。 Can anyone please suggest a solution for this problem. 谁能为这个问题提出解决方案。

The Code where I put values into the map is also given below. 下面也给出了将值放入映射中的代码。

void populateMap() {
    try {
        try {
            objin = new ObjectInputStream(socket.getInputStream());
        } catch (StreamCorruptedException e) {
            System.out.println("Stream Corrupted!");
        }
        Object name = objin.readObject();
        if (((Snake) name).player.equals("food_coord")) {
            Game.foodx = objin.readInt();
            Game.foody = objin.readInt() + 35;
            start = true;
            System.out.println("Game Started");
            return;
        } else {
            map.put(((Snake) name).player, (Snake) name);
            System.out.println("Recieved: " + ((Snake) name).player);
        }
    } catch (java.net.SocketException s) {
        JOptionPane.showMessageDialog(null, "Server Closed", "ERROR",
                JOptionPane.ERROR_MESSAGE);
        System.exit(0);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

将地图实现类更改为支持并发的类,例如ConcurrentHashMap

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM