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.

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
From Robert Klemme <shortcutter@googlemail.com>
Newsgroups comp.lang.java.programmer
Subject Re: Infinite loop from HashMap.keySet iteration.
Date Mon, 18 Jun 2012 04:42:12 -0700 (PDT)
Organization http://groups.google.com
Lines 62
Message-ID <313a279f-0e72-4431-9879-c0deb4385f1c@googlegroups.com> (permalink)
References <EHoCr.13285$ji7.6276@newsfe20.iad>
NNTP-Posting-Host 193.0.246.21
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1
X-Trace posting.google.com 1340019732 4755 127.0.0.1 (18 Jun 2012 11:42:12 GMT)
X-Complaints-To groups-abuse@google.com
NNTP-Posting-Date Mon, 18 Jun 2012 11:42:12 +0000 (UTC)
In-Reply-To <EHoCr.13285$ji7.6276@newsfe20.iad>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=193.0.246.21; posting-account=MGO7qgoAAABvyo26eHVDO00044spH-ws
User-Agent G2/1.0
X-Received-Bytes 3144
Xref csiph.com comp.lang.java.programmer:15377

Show key headers only | 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