简体   繁体   English

ConcurrentModificationException on java.util.Set上的for循环

[英]ConcurrentModificationException on for loop on java.util.Set

This is may be duplicate question but I am a bit confuse in ConcurrentModificationException. 这可能是重复的问题,但我在ConcurrentModificationException中有点困惑。 I gone through some other questions on stack Overflow also some articles related to How to avoid ConcurrentModificationException. 我在堆栈Overflow上经历了其他一些问题,还涉及一些与如何避免ConcurrentModificationException有关的文章。 I come to know that this exception occurs while looping on collection AND modifying the same (most common issue with remove ). 我知道在循环收集并修改相同内容时会发生此异常( remove最常见问题)。

But in my case I am just looping ( foreach ) on an java.util.Set then am I m getting this exception. 但就我而言,我只是在java.util.Set上循环( foreach ),然后出现此异常。

Moreover I am not getting this exception always, When I do load testing of my web application using Jmeter (150 users in 5 seconds) in this scenario I m getting exception 此外,我并非总是会遇到此异常,在这种情况下,当我使用Jmeter (5秒内有150个用户)对Web应用程序进行负载测试时,我正在获取异常

Here is my code where I am getting exception according to stack trace from log file 这是我的代码,根据日志文件中的堆栈跟踪,我在这里得到异常

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;
}

According to log file ConcurrentModificationException is occurred at line for (Object key : objkeys) 根据日志文件ConcurrentModificationException发生在for (Object key : objkeys)

Here is some lines from stack trace 这是堆栈跟踪中的一些行

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)

What can I do to avoid this? 我该怎么做才能避免这种情况? and why this exception is occurring though I am not modifying the set. 以及为什么我没有修改集合却发生此异常。

Updated Code 更新的代码

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

By the guidance from comments on question I changed by code to : 在问题评论的指导下,我将代码更改为:

    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;
}

I made messageMap as static so, this function on first request will check for null if it is null then messageMap get filled then for next request it will directly return messageMap . 我将messageMap为静态,因此,在第一个请求上,此函数将检查null如果为null),然后填充messageMap然后对于下一个请求,它将直接返回messageMap

By this ConcurrentModificationException is resolved. 由此ConcurrentModificationException被解决。

But still If their some suggestion you want to give then Plzz I would like to know. 但是,如果您想给他们一些建议,那么我想知道Plzz。

You modify the messageMap map while iterating over it's keyset. 在迭代messageMap的键集时,可以对其进行修改。 That's the reason you're getting the message. 这就是您收到消息的原因。 Just collect the matching keys inside a ArrayList object, then iterate over that and modify the messageMap map . 只需将匹配的键收集在ArrayList对象中,然后对其进行迭代并修改messageMap映射。

EDIT : 编辑:

the exception is actually referring to the messageMap and not properties . 异常实际上是指messageMap而不是properties Are these 2 related in any way (common keys) . 这2是否以任何方式相关(通用键)。 Is the code not shown here iterating over messageMap too ? 这里的代码也没有遍历messageMap吗?

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

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