java - java.util.Set 中 for 循环中的 ConcurrentModificationException

标签 java spring-mvc concurrentmodification

这可能是重复的问题,但我对 ConcurrentModificationException 有点困惑。我浏览了堆栈溢出上的一些其他问题以及一些与如何避免 ConcurrentModificationException 相关的文章。我了解到,在循环收集并修改相同的集合时会发生此异常(remove 的最常见问题)。

但就我而言,我只是在 java.util.Set 上循环 (foreach),然后我收到此异常。

此外,我并不总是遇到此异常,当我在这种情况下使用 Jmeter (5 秒内 150 个用户)对我的 Web 应用程序进行负载测试时,我遇到了异常

这是我的代码,根据日志文件中的堆栈跟踪,我收到异常

public static Map<String, String> getMessageMap(Locale locale) {
    Properties properties;
    Set<Object> objkeys;
    Map<String, String> messageMap = null;

    if (messageMap == null) {
        messageMap = new HashMap<String, String>();
        // 1 Get properties file.
        properties = Utility.loadPropertiesFile(locale); // this method return properties from static veriable

        // 2 Get all keys of properties file.
        objkeys = properties.keySet();

        // 3 Add all key values into map.
        for (Object key : objkeys) { caught exception here
            String keyName = key.toString();
            if (keyName.contains("global.")) {
                messageMap.put(keyName, properties.getProperty(keyName));
            }
        }

    }
    return messageMap;
}

根据日志文件 ConcurrentModificationException 发生在 for (Object key : objkeys)

这是堆栈跟踪中的一些行

java.util.ConcurrentModificationException
at java.util.Hashtable$Enumerator.next(Unknown Source)
at com.utilities.MKCLUtility.getMessageMap(Utility.java:164)
at com.utilities.MKCLUtility.addMessage(Utility.java:49)
at com.controllers.LoginController.loginPost(LoginController.java:132)

我可以做什么来避免这种情况?以及为什么尽管我没有修改该集合,但还是发生了此异常。

更新代码

Iterator<Object> iterator = objkeys.iterator();
while (iterator.hasNext()) 
{
    String keyName = iterator.next().toString();
    if (keyName.contains("global.")) {
        messageMap.put(keyName, properties.getProperty(keyName));
    }
}

最佳答案

根据问题评论的指导,我将代码更改为:

    public static Map<String, String> getMessageMap(Locale locale) {
    Properties properties;
    Set<Object> objkeys;


    if (messageMap == null) {
        messageMap = new HashMap<String, String>();
        // 1 Get properties file.
        properties = MKCLUtility.loadPropertiesFile(locale);

        // 2 Get all keys of properties file.
        objkeys = properties.keySet();

        // 3 Add all key values into map.
        Iterator<Object> iterator = objkeys.iterator();
        while (iterator.hasNext()) 
        {
            String keyName = iterator.next().toString();
            if (keyName.contains("global.")) {
                messageMap.put(keyName, properties.getProperty(keyName));
            }
        }
    }
    return messageMap;
}

我将 messageMap 设置为静态,因此,第一次请求时该函数将检查 null 如果它为空,则 messageMap 被填充,然后为下一个请求将直接返回messageMap

通过此ConcurrentModificationException 已解决。

但是如果您想提出一些建议,那么我想知道。

关于java - java.util.Set 中 for 循环中的 ConcurrentModificationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24362210/

相关文章:

spring-mvc - Spring MVC 和 JSR 303 自定义验证与自定义资源包

java - 当 Hibernate 从 4.x 升级到 5.x 以及 Spring 4.x 进行多线程时出现 ConcurrentModificationException

java - 如何解决更新子元素时的ConcurrentModificationException?

java - 如何将 byte[] 保存为 java.sql.Clob?

java - Spring 新手 - 使用 sec 标签不起作用

Java DatagramSocket 在初始化时卡住

java - 上传文件时出现 "Could not delete file C:\Users\..."错误

java - 如何处理 ConcurrentModificationException

c# - 在Android中POST后从WCF返回JSON

java - for循环Java返回值