<?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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: &quot;Copy-on-Iterate&quot; Java Idiom considered broken</title>
	<atom:link href="http://zeroturnaround.com/blog/copy-on-iterate-java-idiom-considered-broken/feed/" rel="self" type="application/rss+xml" />
	<link>http://zeroturnaround.com/blog/copy-on-iterate-java-idiom-considered-broken/</link>
	<description>Improving Java Development</description>
	<lastBuildDate>Wed, 16 May 2012 14:41:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
	<item>
		<title>By: ckbe</title>
		<link>http://zeroturnaround.com/blog/copy-on-iterate-java-idiom-considered-broken/comment-page-1/#comment-787</link>
		<dc:creator>ckbe</dc:creator>
		<pubDate>Thu, 29 Jul 2010 11:57:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.zeroturnaround.com/?p=2939#comment-787</guid>
		<description>Well the only upside of a copy is that you get a snapshot of the  CHM at one point in time. &lt;br&gt;&lt;br&gt;But this idea of an snapshot isn&#039;t really supported by the CHM. &lt;br&gt;&lt;br&gt;So best is, not use a CHM or use it&#039;s iterator an be aware, that the iterator returns Objects that have been in the CHM between construction of the iterator and the end of the loop.</description>
		<content:encoded><![CDATA[<p>Well the only upside of a copy is that you get a snapshot of the  CHM at one point in time. </p>
<p>But this idea of an snapshot isn&#39;t really supported by the CHM. </p>
<p>So best is, not use a CHM or use it&#39;s iterator an be aware, that the iterator returns Objects that have been in the CHM between construction of the iterator and the end of the loop.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ron</title>
		<link>http://zeroturnaround.com/blog/copy-on-iterate-java-idiom-considered-broken/comment-page-1/#comment-786</link>
		<dc:creator>ron</dc:creator>
		<pubDate>Thu, 29 Jul 2010 01:32:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.zeroturnaround.com/?p=2939#comment-786</guid>
		<description>I do not see the need for any copying here, after all it is a CHM we are working on. As the doc says:&lt;br&gt;&lt;br&gt;&quot;The view&#039;s iterator is a &quot;weakly consistent&quot; iterator that will never throw ConcurrentModificationException, and guarantees to traverse elements as they existed upon construction of the iterator, and may (but is not guaranteed to) reflect any modifications subsequent to construction.&quot;&lt;br&gt;&lt;br&gt;So i won&#039;t have any trouble directly iterating on modifiableToEntry.values(). Of course, the above implies, that the underlying collection might get modified during iteration, which might have implications on application logic, but it will not throw any Exception. &lt;br&gt;&lt;br&gt;Basically this means that if an entry is inserted or removed at a segment or index in the CHM which the iterator has already seen, nothing happens. If an entry is inserted or removed at a segment or index in the CHM which the iterator has not seen yet, the iterator will just deliver the new entry (or not deliver a removed entry). &lt;br&gt;&lt;br&gt;I also want to mention, that Collections.synchronizedCollection does not provide a synchronized iterator() method, either. The user has to manually synchronize iterator creation, which, as opposed to CHM, is easily possible by syncronizing on the Collection itself (or providing a mutex in the Collections.synchronizedCollection call)</description>
		<content:encoded><![CDATA[<p>I do not see the need for any copying here, after all it is a CHM we are working on. As the doc says:</p>
<p>&#8220;The view&#39;s iterator is a &#8220;weakly consistent&#8221; iterator that will never throw ConcurrentModificationException, and guarantees to traverse elements as they existed upon construction of the iterator, and may (but is not guaranteed to) reflect any modifications subsequent to construction.&#8221;</p>
<p>So i won&#39;t have any trouble directly iterating on modifiableToEntry.values(). Of course, the above implies, that the underlying collection might get modified during iteration, which might have implications on application logic, but it will not throw any Exception. </p>
<p>Basically this means that if an entry is inserted or removed at a segment or index in the CHM which the iterator has already seen, nothing happens. If an entry is inserted or removed at a segment or index in the CHM which the iterator has not seen yet, the iterator will just deliver the new entry (or not deliver a removed entry). </p>
<p>I also want to mention, that Collections.synchronizedCollection does not provide a synchronized iterator() method, either. The user has to manually synchronize iterator creation, which, as opposed to CHM, is easily possible by syncronizing on the Collection itself (or providing a mutex in the Collections.synchronizedCollection call)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jevgeni Kabanov</title>
		<link>http://zeroturnaround.com/blog/copy-on-iterate-java-idiom-considered-broken/comment-page-1/#comment-785</link>
		<dc:creator>Jevgeni Kabanov</dc:creator>
		<pubDate>Wed, 28 Jul 2010 03:37:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.zeroturnaround.com/?p=2939#comment-785</guid>
		<description>In this case the underlying collection is assumed to be synchronized, so toArray() is also synchronized and isn&#039;t exposed to anything.</description>
		<content:encoded><![CDATA[<p>In this case the underlying collection is assumed to be synchronized, so toArray() is also synchronized and isn&#39;t exposed to anything.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrey</title>
		<link>http://zeroturnaround.com/blog/copy-on-iterate-java-idiom-considered-broken/comment-page-1/#comment-783</link>
		<dc:creator>Andrey</dc:creator>
		<pubDate>Wed, 28 Jul 2010 01:43:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.zeroturnaround.com/?p=2939#comment-783</guid>
		<description>I meant reading/modifying :)  &lt;br&gt;Any way what for to copy the collection values? why not to iterate directly over CHM?</description>
		<content:encoded><![CDATA[<p>I meant reading/modifying :)  <br />Any way what for to copy the collection values? why not to iterate directly over CHM?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ckbe</title>
		<link>http://zeroturnaround.com/blog/copy-on-iterate-java-idiom-considered-broken/comment-page-1/#comment-781</link>
		<dc:creator>ckbe</dc:creator>
		<pubDate>Wed, 28 Jul 2010 01:21:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.zeroturnaround.com/?p=2939#comment-781</guid>
		<description>Easiest fix would be: &lt;br&gt;Use a Collections.synchronizedMap(modifiableToEntry) and a synchronized(modifiableToEntry) {} block round the ArrayList Constructor.</description>
		<content:encoded><![CDATA[<p>Easiest fix would be: <br />Use a Collections.synchronizedMap(modifiableToEntry) and a synchronized(modifiableToEntry) {} block round the ArrayList Constructor.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ckbe</title>
		<link>http://zeroturnaround.com/blog/copy-on-iterate-java-idiom-considered-broken/comment-page-1/#comment-782</link>
		<dc:creator>ckbe</dc:creator>
		<pubDate>Wed, 28 Jul 2010 01:16:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.zeroturnaround.com/?p=2939#comment-782</guid>
		<description>Not that easy. Each and every use (modification or read) of the ConcurrentHashMap must then be synchronized, since the CHM doesn&#039;t use itself as a mutex. And then you got a &quot;NonConcurrentButTwiceSynchronizedHashMap&quot; .. ;-)</description>
		<content:encoded><![CDATA[<p>Not that easy. Each and every use (modification or read) of the ConcurrentHashMap must then be synchronized, since the CHM doesn&#39;t use itself as a mutex. And then you got a &#8220;NonConcurrentButTwiceSynchronizedHashMap&#8221; .. ;-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ckbe</title>
		<link>http://zeroturnaround.com/blog/copy-on-iterate-java-idiom-considered-broken/comment-page-1/#comment-784</link>
		<dc:creator>ckbe</dc:creator>
		<pubDate>Wed, 28 Jul 2010 01:07:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.zeroturnaround.com/?p=2939#comment-784</guid>
		<description>The difference is, that if you decide at one point to use a synchronized() on a ConcurrentHashMap, you must synchronize each and every access explicitly and loose the benefit of the ConcurrentHashMap (The concurrency).&lt;br&gt;Whereas if you use the simpler synchronizedMap() and just do a synchronized() block around for example the iteration loop (or the constructor call of the ArrayList) everything works fine.</description>
		<content:encoded><![CDATA[<p>The difference is, that if you decide at one point to use a synchronized() on a ConcurrentHashMap, you must synchronize each and every access explicitly and loose the benefit of the ConcurrentHashMap (The concurrency).<br />Whereas if you use the simpler synchronizedMap() and just do a synchronized() block around for example the iteration loop (or the constructor call of the ArrayList) everything works fine.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeffrey Smith</title>
		<link>http://zeroturnaround.com/blog/copy-on-iterate-java-idiom-considered-broken/comment-page-1/#comment-779</link>
		<dc:creator>Jeffrey Smith</dc:creator>
		<pubDate>Tue, 27 Jul 2010 23:51:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.zeroturnaround.com/?p=2939#comment-779</guid>
		<description>By the way, your purported solution won&#039;t work, because toArray() is not atomic.&lt;br&gt;&lt;br&gt;Changing:&lt;br&gt;collection.toArray(array);&lt;br&gt;&lt;br&gt;to:&lt;br&gt;array = collection.toArray(array);&lt;br&gt;&lt;br&gt;will not avoid an array indexing error within the toArray() method. The exposure&lt;br&gt;to a concurrent modification is still present.&lt;br&gt;&lt;br&gt;The assignments:&lt;br&gt;collection.toArray(array);&lt;br&gt;lastIndex = size;&lt;br&gt;&lt;br&gt;should at least be changed to:&lt;br&gt;array = collection.toArray(array);&lt;br&gt;lastIndex = array.length;&lt;br&gt;&lt;br&gt;but there is still an exposure within toArray() while constructing the&lt;br&gt;copy.&lt;br&gt;&lt;br&gt;The simple solution is not to use unsynchronized collections within a&lt;br&gt;multi-threaded context.</description>
		<content:encoded><![CDATA[<p>By the way, your purported solution won&#39;t work, because toArray() is not atomic.</p>
<p>Changing:<br />collection.toArray(array);</p>
<p>to:<br />array = collection.toArray(array);</p>
<p>will not avoid an array indexing error within the toArray() method. The exposure<br />to a concurrent modification is still present.</p>
<p>The assignments:<br />collection.toArray(array);<br />lastIndex = size;</p>
<p>should at least be changed to:<br />array = collection.toArray(array);<br />lastIndex = array.length;</p>
<p>but there is still an exposure within toArray() while constructing the<br />copy.</p>
<p>The simple solution is not to use unsynchronized collections within a<br />multi-threaded context.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeffrey Smith</title>
		<link>http://zeroturnaround.com/blog/copy-on-iterate-java-idiom-considered-broken/comment-page-1/#comment-780</link>
		<dc:creator>Jeffrey Smith</dc:creator>
		<pubDate>Tue, 27 Jul 2010 23:38:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.zeroturnaround.com/?p=2939#comment-780</guid>
		<description>The Collections.toArray() methods must be internally synchronized, or else they&lt;br&gt;will suffer the same fate. I suggest you look at their 1.6 implementations to verify&lt;br&gt;there is no internal race condition. I believe there is a race condition in the&lt;br&gt;ArrayList.toArray() methods, because that class is specifically documented as&lt;br&gt;not thread-safe. The internal changes to &quot;elementData&quot; and &quot;size&quot; fields are&lt;br&gt;separated in time without synchronization, and therefore are not thread-safe.&lt;br&gt;&lt;br&gt;If there is no way to freeze changes from all threads for the collection while a copy&lt;br&gt;is constructed, then using the Iterator is clearly a client design flaw. The Iterator&lt;br&gt;API is not intended for multi-threaded access. The Collections.toArray() methods&lt;br&gt;are not atomic, unless the implementation specifically states that it is atomic.&lt;br&gt;&lt;br&gt;ConcurrentModificationException thrown by the Iterator is intended solely to detect client&lt;br&gt;design flaws and bugs during application development. The ArrayIndexOutOfBoundsException&lt;br&gt;clearly shows that the client application is incorrectly using an ArrayList in a multi-threaded&lt;br&gt;context.</description>
		<content:encoded><![CDATA[<p>The Collections.toArray() methods must be internally synchronized, or else they<br />will suffer the same fate. I suggest you look at their 1.6 implementations to verify<br />there is no internal race condition. I believe there is a race condition in the<br />ArrayList.toArray() methods, because that class is specifically documented as<br />not thread-safe. The internal changes to &#8220;elementData&#8221; and &#8220;size&#8221; fields are<br />separated in time without synchronization, and therefore are not thread-safe.</p>
<p>If there is no way to freeze changes from all threads for the collection while a copy<br />is constructed, then using the Iterator is clearly a client design flaw. The Iterator<br />API is not intended for multi-threaded access. The Collections.toArray() methods<br />are not atomic, unless the implementation specifically states that it is atomic.</p>
<p>ConcurrentModificationException thrown by the Iterator is intended solely to detect client<br />design flaws and bugs during application development. The ArrayIndexOutOfBoundsException<br />clearly shows that the client application is incorrectly using an ArrayList in a multi-threaded<br />context.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jevgeni Kabanov</title>
		<link>http://zeroturnaround.com/blog/copy-on-iterate-java-idiom-considered-broken/comment-page-1/#comment-778</link>
		<dc:creator>Jevgeni Kabanov</dc:creator>
		<pubDate>Tue, 27 Jul 2010 21:58:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.zeroturnaround.com/?p=2939#comment-778</guid>
		<description>What difference does it make? Yes, CHM uses multiple monitors to sync, but semantically the result is the same, if faster.</description>
		<content:encoded><![CDATA[<p>What difference does it make? Yes, CHM uses multiple monitors to sync, but semantically the result is the same, if faster.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

