Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #15377

Re: Infinite loop from HashMap.keySet iteration.

From Robert Klemme <shortcutter@googlemail.com>
Newsgroups comp.lang.java.programmer
Subject Re: Infinite loop from HashMap.keySet iteration.
Date 2012-06-18 04:42 -0700
Organization http://groups.google.com
Message-ID <313a279f-0e72-4431-9879-c0deb4385f1c@googlegroups.com> (permalink)
References <EHoCr.13285$ji7.6276@newsfe20.iad>

Show all headers | View raw


On Thursday, June 14, 2012 6:54:59 PM UTC+2, Daniel Pitts wrote:
> Just wanted to share some experience with the group.
> 
> I've come across a very strange situation, and I think I have the 
> solution (still needs to be verified in production, but looks promising).
> 
> I've inherited some code which basically does the following. Obfuscated 
> and simplified for demonstration purposes.
> 
> void logResults(Map<String, Result> resultsMap) {
>     StringBuilder logLine = new StringBuilder("Some stuff");
>     Set<String> keySet = resultsMap.keySet();
>     for (String key: keySet) {
>        logLine.append(key).append(": ").append(resultsMap.get(key));
>     }
>     logger.info(logLine.toString());
> }

That should look roughly like this (more efficient to use the entrySet() and synchronize added):

void logResults(Map<String, Result> resultsMap) {
    final StringBuilder logLine = new StringBuilder("Some stuff");

    synchronized (resultsMap) {
      for (final Entry<String, Result> entry: resultsMap.entrySet()) {
         logLine.append(entry.getKey()).append(": ").append(entry.getValue());
      }
    }

    logger.info(logLine.toString());
}

> void addResult(ResultFactory factory, Map<String, Result> resultsMap) {
>     synchronize(factory) {
>         Result result;
>         result = resultsMap.get(factory.getName());
>         if (result == null) {
>              result = new Result();
>              resultsMap.put(factory.getName(), result);
>         }
>         result.recordResult(factory.getResult());
>     }
> }

It looks like there is a spelling error and it was intended to be

void addResult(ResultFactory factory, Map<String, Result> resultsMap) {
    synchronize(resultsMap) { // error fixed
        Result result = resultsMap.get(factory.getName());
        if (result == null) {
             result = new Result();
             resultsMap.put(factory.getName(), result);
        }
        result.recordResult(factory.getResult());
    }
}

Or the synchronize(resultsMap) was simply forgotten.

Kind regards

robert

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Infinite loop from HashMap.keySet iteration. Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-06-14 09:54 -0700
  Re: Infinite loop from HashMap.keySet iteration. markspace <-@.> - 2012-06-14 10:17 -0700
    Re: Infinite loop from HashMap.keySet iteration. Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-06-14 10:34 -0700
  Re: Infinite loop from HashMap.keySet iteration. Roedy Green <see_website@mindprod.com.invalid> - 2012-06-14 11:34 -0700
    Re: Infinite loop from HashMap.keySet iteration. Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-06-14 12:19 -0700
  Re: Infinite loop from HashMap.keySet iteration. Robert Klemme <shortcutter@googlemail.com> - 2012-06-18 04:42 -0700
    Re: Infinite loop from HashMap.keySet iteration. Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-06-18 09:08 -0700
      Re: Infinite loop from HashMap.keySet iteration. Robert Klemme <shortcutter@googlemail.com> - 2012-06-18 18:34 +0200
        Re: Infinite loop from HashMap.keySet iteration. Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-06-18 13:32 -0700
          Re: Infinite loop from HashMap.keySet iteration. Robert Klemme <shortcutter@googlemail.com> - 2012-06-18 23:26 +0200
            Re: Infinite loop from HashMap.keySet iteration. Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-06-18 15:30 -0700

csiph-web