<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: HashMap.get() can cause an infinite loop!</title>
	<atom:link href="http://lightbody.net/blog/2005/07/hashmapget_can_cause_an_infini.html/feed" rel="self" type="application/rss+xml" />
	<link>http://lightbody.net/blog/2005/07/hashmapget_can_cause_an_infini.html</link>
	<description>Patrick Lightbody's personal blog</description>
	<pubDate>Sat, 22 Nov 2008 05:19:33 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
		<item>
		<title>By: Brian Egge</title>
		<link>http://lightbody.net/blog/2005/07/hashmapget_can_cause_an_infini.html#comment-1028</link>
		<dc:creator>Brian Egge</dc:creator>
		<pubDate>Fri, 03 Oct 2008 02:25:34 +0000</pubDate>
		<guid isPermaLink="false">http://lightbody.net/blog/2005/7/hashmapget_can_cause_an_infini.html#comment-1028</guid>
		<description>A similar, but no less fun, loop can occur within DateFormat.  Concurrent use generally leads to garbage in your formatted dates, but can upon occasion cause the DateFormat to get caught in a loop.  

If multiple threads access a formatter concurrently, it must be synchronized externally.

I feel the 'must' in the JavaDoc is a pretty good warning to say your worst nightmares may come true if you break the rule.  Because the implementation of a class is allowed to change, the JavaDoc shouldn't have to specify exactly what the worst case scenario is.  In fact, if you haven't designed a class with concurrency in mind, it can be really hard to anticipate what the worst case scenario is.</description>
		<content:encoded><![CDATA[<p>A similar, but no less fun, loop can occur within DateFormat.  Concurrent use generally leads to garbage in your formatted dates, but can upon occasion cause the DateFormat to get caught in a loop.  </p>
<p>If multiple threads access a formatter concurrently, it must be synchronized externally.</p>
<p>I feel the &#8216;must&#8217; in the JavaDoc is a pretty good warning to say your worst nightmares may come true if you break the rule.  Because the implementation of a class is allowed to change, the JavaDoc shouldn&#8217;t have to specify exactly what the worst case scenario is.  In fact, if you haven&#8217;t designed a class with concurrency in mind, it can be really hard to anticipate what the worst case scenario is.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark L</title>
		<link>http://lightbody.net/blog/2005/07/hashmapget_can_cause_an_infini.html#comment-969</link>
		<dc:creator>Mark L</dc:creator>
		<pubDate>Fri, 29 Aug 2008 20:41:57 +0000</pubDate>
		<guid isPermaLink="false">http://lightbody.net/blog/2005/7/hashmapget_can_cause_an_infini.html#comment-969</guid>
		<description>I did convert to ConcurrentHashMap which is in new java.util.concurrent collections. I performed a few tests to check for memory and performance differences of both HashMap and ConcurrentHashMap  and they appeared to me to perform roughly about the same. 

ConcurrentHashMap sure has solved my problem with this.

Thanks for your original post all them years ago Patrick.</description>
		<content:encoded><![CDATA[<p>I did convert to ConcurrentHashMap which is in new java.util.concurrent collections. I performed a few tests to check for memory and performance differences of both HashMap and ConcurrentHashMap  and they appeared to me to perform roughly about the same. </p>
<p>ConcurrentHashMap sure has solved my problem with this.</p>
<p>Thanks for your original post all them years ago Patrick.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: plightbo</title>
		<link>http://lightbody.net/blog/2005/07/hashmapget_can_cause_an_infini.html#comment-967</link>
		<dc:creator>plightbo</dc:creator>
		<pubDate>Fri, 29 Aug 2008 14:58:02 +0000</pubDate>
		<guid isPermaLink="false">http://lightbody.net/blog/2005/7/hashmapget_can_cause_an_infini.html#comment-967</guid>
		<description>Maninder,
You'll have to synchronize it. It shouldn't really cause much of a hit, so just go for it. Or, use the new java.util.concurrent collections and they'll manage it for you. Good luck!</description>
		<content:encoded><![CDATA[<p>Maninder,<br />
You&#8217;ll have to synchronize it. It shouldn&#8217;t really cause much of a hit, so just go for it. Or, use the new java.util.concurrent collections and they&#8217;ll manage it for you. Good luck!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Maninder Batth</title>
		<link>http://lightbody.net/blog/2005/07/hashmapget_can_cause_an_infini.html#comment-966</link>
		<dc:creator>Maninder Batth</dc:creator>
		<pubDate>Fri, 29 Aug 2008 14:56:01 +0000</pubDate>
		<guid isPermaLink="false">http://lightbody.net/blog/2005/7/hashmapget_can_cause_an_infini.html#comment-966</guid>
		<description>Patrick,
All i can say is i am speechless. I made exact same assumptions as you because, in my application too, the risk of data corruption  was managable and easily rectified, hence i choose not to synchronize hashmap and the remainder is the history.
BTW, can you please suggest a cure... I still do not want to synchronize, atleast not externally.</description>
		<content:encoded><![CDATA[<p>Patrick,<br />
All i can say is i am speechless. I made exact same assumptions as you because, in my application too, the risk of data corruption  was managable and easily rectified, hence i choose not to synchronize hashmap and the remainder is the history.<br />
<span class="caps">BTW, </span>can you please suggest a cure&#8230; I still do not want to synchronize, atleast not externally.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark L</title>
		<link>http://lightbody.net/blog/2005/07/hashmapget_can_cause_an_infini.html#comment-939</link>
		<dc:creator>Mark L</dc:creator>
		<pubDate>Tue, 19 Aug 2008 04:41:54 +0000</pubDate>
		<guid isPermaLink="false">http://lightbody.net/blog/2005/7/hashmapget_can_cause_an_infini.html#comment-939</guid>
		<description>I too assumed that the data was safe, much to my peril!

I am now converting to ConcurrentHashMap. I am assuming this will resolve the issue - has anybody found this not to be the case?</description>
		<content:encoded><![CDATA[<p>I too assumed that the data was safe, much to my peril!</p>
<p>I am now converting to ConcurrentHashMap. I am assuming this will resolve the issue - has anybody found this not to be the case?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stefan M</title>
		<link>http://lightbody.net/blog/2005/07/hashmapget_can_cause_an_infini.html#comment-900</link>
		<dc:creator>Stefan M</dc:creator>
		<pubDate>Wed, 23 Jul 2008 15:27:53 +0000</pubDate>
		<guid isPermaLink="false">http://lightbody.net/blog/2005/7/hashmapget_can_cause_an_infini.html#comment-900</guid>
		<description>"Note that this implementation is not synchronized. If multiple threads access this map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally."

I atleast would like to know the why, before someone tells me "You must". Im grown up and dont want to be treated like a child by these "great programmers". Im grateful to the author for pointing out the specific problem and hope that all these top down arguers will run into a similar problem themselves. 

@MAno "I think data integrity is one of the basic goals when writing ANY kind of real-world application" thats your opinion and you may want to consider that there are other valid opinions out there than just yours.

I agree fully with the author who is not blaming anyone, that the JavaDoc COULD be more verbose than a simple: "you must".</description>
		<content:encoded><![CDATA[<p>&#8220;Note that this implementation is not synchronized. If multiple threads access this map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally.&#8221;</p>
<p>I atleast would like to know the why, before someone tells me &#8220;You must&#8221;. Im grown up and dont want to be treated like a child by these &#8220;great programmers&#8221;. Im grateful to the author for pointing out the specific problem and hope that all these top down arguers will run into a similar problem themselves. </p>
<p>@MAno &#8220;I think data integrity is one of the basic goals when writing <span class="caps">ANY </span>kind of real-world application&#8221; thats your opinion and you may want to consider that there are other valid opinions out there than just yours.</p>
<p>I agree fully with the author who is not blaming anyone, that the JavaDoc <span class="caps">COULD </span>be more verbose than a simple: &#8220;you must&#8221;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: MAno F.</title>
		<link>http://lightbody.net/blog/2005/07/hashmapget_can_cause_an_infini.html#comment-875</link>
		<dc:creator>MAno F.</dc:creator>
		<pubDate>Fri, 04 Jul 2008 15:21:03 +0000</pubDate>
		<guid isPermaLink="false">http://lightbody.net/blog/2005/7/hashmapget_can_cause_an_infini.html#comment-875</guid>
		<description>One more note: trying to avoid the consequences of unsynchronized access to HashMap would certainly result in more time spent in get() method (and maybe other methods too), but the reason the HashMap is not synchronized (unlike Hashtable) is exactly: performance.</description>
		<content:encoded><![CDATA[<p>One more note: trying to avoid the consequences of unsynchronized access to HashMap would certainly result in more time spent in get() method (and maybe other methods too), but the reason the HashMap is not synchronized (unlike Hashtable) is exactly: performance.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: MAno F.</title>
		<link>http://lightbody.net/blog/2005/07/hashmapget_can_cause_an_infini.html#comment-874</link>
		<dc:creator>MAno F.</dc:creator>
		<pubDate>Fri, 04 Jul 2008 15:12:59 +0000</pubDate>
		<guid isPermaLink="false">http://lightbody.net/blog/2005/7/hashmapget_can_cause_an_infini.html#comment-874</guid>
		<description>I'm just curious, what kind of application did you wrote, if you did not care about data integrity. I think data integrity is one of the basic goals when writing ANY kind of real-world application (well, except of concurency tests &#38; multithreading-learning/teaching purposes).

As I read the JavaDoc again and again, there was no mention about data integrity. It just says: "if at least one thread makes structural modification, HashMap MUST BE synchronized". No word about the possible consequences (not only "data integrity"), so this means "ANYTHING COULD HAPPEN, INCLUSIVE THINGS YOU DON'T EXPECT, when you don't synchronize mutable HashMap accessed from more than one thread". Inifinite loop is one possible consequence of that.

Well, even the Collection Framework programmers do not know what everything could happen when you don't keep their warning in mind, so why do they should write down every possible harm the unsynchronized usage of HashMap (accessed from several threads) could cause?

For example - another possible damage I see is losing references to some stored objects when reordering the same linked list concurrently in two or more threads (which is the case this time).

So unless you are just doing harmless experiments, I would suggest YOU ALWAYS SHOULD CARE ABOUT DATA INTEGRITY/CONSISTENCY.</description>
		<content:encoded><![CDATA[<p>I&#8217;m just curious, what kind of application did you wrote, if you did not care about data integrity. I think data integrity is one of the basic goals when writing <span class="caps">ANY </span>kind of real-world application (well, except of concurency tests &amp; multithreading-learning/teaching purposes).</p>
<p>As I read the JavaDoc again and again, there was no mention about data integrity. It just says: &#8220;if at least one thread makes structural modification, HashMap <span class="caps">MUST</span> BE synchronized&#8221;. No word about the possible consequences (not only &#8220;data integrity&#8221;), so this means &#8220;ANYTHING <span class="caps">COULD HAPPEN, INCLUSIVE THINGS YOU DON&#8217;T EXPECT, </span>when you don&#8217;t synchronize mutable HashMap accessed from more than one thread&#8221;. Inifinite loop is one possible consequence of that.</p>
<p>Well, even the Collection Framework programmers do not know what everything could happen when you don&#8217;t keep their warning in mind, so why do they should write down every possible harm the unsynchronized usage of HashMap (accessed from several threads) could cause?</p>
<p>For example - another possible damage I see is losing references to some stored objects when reordering the same linked list concurrently in two or more threads (which is the case this time).</p>
<p>So unless you are just doing harmless experiments, I would suggest <span class="caps">YOU ALWAYS SHOULD CARE ABOUT DATA INTEGRITY</span>/CONSISTENCY.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Concurrency and HashMap</title>
		<link>http://lightbody.net/blog/2005/07/hashmapget_can_cause_an_infini.html#comment-873</link>
		<dc:creator>Concurrency and HashMap</dc:creator>
		<pubDate>Wed, 02 Jul 2008 13:45:04 +0000</pubDate>
		<guid isPermaLink="false">http://lightbody.net/blog/2005/7/hashmapget_can_cause_an_infini.html#comment-873</guid>
		<description>[...] You can read details about the infinite loop problem here. [...]</description>
		<content:encoded><![CDATA[<p>[...] You can read details about the infinite loop problem here. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bakuras</title>
		<link>http://lightbody.net/blog/2005/07/hashmapget_can_cause_an_infini.html#comment-872</link>
		<dc:creator>Bakuras</dc:creator>
		<pubDate>Wed, 02 Jul 2008 09:34:45 +0000</pubDate>
		<guid isPermaLink="false">http://lightbody.net/blog/2005/7/hashmapget_can_cause_an_infini.html#comment-872</guid>
		<description>The fact that "you don't care about integrity of your data" does not mean that you don't care about if your program will terminate. The problem is that javadoc states explicitly that the behavior of unsynchronized HashMap is UNPREDICTABLE (aka non deterministic) which also means that your program probably will not terminate at all.

If HashMap is used correctly you will get correct behavior, since it is tested by millions and written by great programmers. So complaining about wrong usage of means is really pointless. You know fire have many usages ...

If you don't care about integrity of your data why not to use some random bias if you want simulate non-deterministic behaviors.</description>
		<content:encoded><![CDATA[<p>The fact that &#8220;you don&#8217;t care about integrity of your data&#8221; does not mean that you don&#8217;t care about if your program will terminate. The problem is that javadoc states explicitly that the behavior of unsynchronized HashMap is <span class="caps">UNPREDICTABLE </span>(aka non deterministic) which also means that your program probably will not terminate at all.</p>
<p>If HashMap is used correctly you will get correct behavior, since it is tested by millions and written by great programmers. So complaining about wrong usage of means is really pointless. You know fire have many usages &#8230;</p>
<p>If you don&#8217;t care about integrity of your data why not to use some random bias if you want simulate non-deterministic behaviors.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
