并发修改异常 (Java)

ConcurrentModificationException (Java)(并发修改异常 (Java))
本文介绍了并发修改异常 (Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

Exception in thread "main" java.util.ConcurrentModificationException
Squash the PC dirties the room Violet. The room's state is now dirty
Lily the animal growls
The Animal Lily left the room and goes to Green through the west door.
        at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793)
        at java.util.HashMap$KeyIterator.next(HashMap.java:828)
        at homework5.Room.critReactRoomStateChange(Room.java:76)
        at homework5.PC.play(PC.java:121)
        at homework5.Main.main(Main.java:41)
Java Result: 1

这是我收到的错误.

我的方法看起来像

public void critReactRoomStateChange(String command, PC pc) {
    Creature temp = null;
    Iterator iterator = getCreatures().keySet().iterator();
    while (iterator.hasNext()) {
        String names = iterator.next().toString();
        if (!(getCreatures().get(names) instanceof PC)) {
            temp = getCreatures().get(names);
            if (temp != null) {
                temp.reactStateChange(command, pc);
                temp.checkNewRoom();
            }
        }
    }
} 

所以我的理解是,这意味着我要在迭代器完成之前更改它的大小,这就是你得到的错误.这是正确的,因为 reactStateChange 之一是要从 hashMap 中删除对象.如何安全地执行此操作,以便在删除某些内容时让迭代器提前知道,这样我就可以避免此错误.提前致谢.如果需要更多详细信息,我很乐意满足您的要求.

So what I understand is this means I'm changing the size of the iterator before it is finished and this is the error you get. This is true as one of the reactStateChange is for an object to be removed out of the hashMap. How do I do this safely so that when I remove something it lets the Iterator know ahead of time so I can avoid this error. Thanks in advance. If more details are needed I'd be glad to meet your requests.

推荐答案

从底层集合中删除元素并继续迭代的唯一安全方法是使用 的 com/javase/6/docs/api/java/util/Iterator.html#remove%28%29" rel="noreferrer">remove() 方法迭代器.这将删除 Iteratornext() 方法返回的最后一个元素.

The only safe way to remove an element from an underlying collection and continue the iteration is to use the remove() method of the Iterator. This removes the last element returned by the next() method of the Iterator.

在您的情况下,这似乎需要将 Iterator 传递给执行修改的方法(或使其成为实例字段,如 Map 对象是已经).

In your case, it appears that this would require passing the Iterator to the method that performs the modification (or make it an instance field, like the Map object is already).

这篇关于并发修改异常 (Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Reliable implementation of PBKDF2-HMAC-SHA256 for JAVA(PBKDF2-HMAC-SHA256 for JAVA 的可靠实现)
Correct way to sign and verify signature using bouncycastle(使用 bouncycastle 签名和验证签名的正确方法)
Creating RSA Public Key From String(从字符串创建 RSA 公钥)
Why java.security.NoSuchProviderException No such provider: BC?(为什么 java.security.NoSuchProviderException 没有这样的提供者:BC?)
Generating X509 Certificate using Bouncy Castle Java(使用 Bouncy Castle Java 生成 X509 证书)
How can I get a PublicKey object from EC public key bytes?(如何从 EC 公钥字节中获取 PublicKey 对象?)