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


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

Re: Infinite loop from HashMap.keySet iteration.

From markspace <-@.>
Newsgroups comp.lang.java.programmer
Subject Re: Infinite loop from HashMap.keySet iteration.
Date 2012-06-14 10:17 -0700
Organization A noiseless patient Spider
Message-ID <jrd6b5$mh1$1@dont-email.me> (permalink)
References <EHoCr.13285$ji7.6276@newsfe20.iad>

Show all headers | View raw


Well, it doesn't look "safe" to me because you are reading 
unsynchronized from a shared object in the logResults() method.  Always 
always always you must synchronized both the read and the write, or it's 
not thread safe.

You're also not MUTEX safe either, which clearly needs to be dealt with. 
  If you were using some form of Collections iterator, you'd probably be 
getting concurrent modification exceptions.

For the MUTEX/concurrent modification problem, I don't think using 
Collections.synchronizedMap() is enough.  You're going to have to block 
all access during the read.  This could ruin the concurrency of your 
app, so there are even further issues here to consider, but strictly 
from a thread safety issue it's the better solution I think.

void logResults(Map<String, Result> resultsMap) {
  StringBuilder logLine = new StringBuilder("Some stuff");
  synchronized( resultsMap ) {
    Set<String> keySet = resultsMap.keySet();
    for (String key: keySet) {
       logLine.append(key).append(": ").append(resultsMap.get(key));
    }
  }
  logger.info(logLine.toString());
}

Assuming "Some stuff" is thread safe.

However I think a different scheme altogether is needed.  Something like 
a ConcurrentLinkedQueue rather than a Map is probably an even better 
solution overall, from both an application performance & concurrency 
standpoint, and a thread safe & correct standpoint.

I realize you say your app is legacy.  I'm just tossing ideas out for 
future reference.

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