tag:blogger.com,1999:blog-8712770457197348465.post7321451680832646845..comments2024-03-17T23:01:26.872-07:00Comments on Javarevisited: Difference between fail-safe vs fail-fast Iterator in Java? Examplejavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger20125tag:blogger.com,1999:blog-8712770457197348465.post-20238068304344561512018-02-12T19:13:49.774-08:002018-02-12T19:13:49.774-08:00The behavior described by @james is a bug in the J...The behavior described by @james is a bug in the JDK. Basically, removing any element from the list during the second-to-last iteration will cause hasNext() to return false and don't call next() again, thus never calling checkForComodification(), which is the method responsible for throwing ConcurrentModificationException.<br /><br />Although Oracle has acknowledged the bug, they also Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-16931683029458996492017-07-29T01:05:27.138-07:002017-07-29T01:05:27.138-07:00@Shashi Kumar That IllegalStateException is becaus...@Shashi Kumar That IllegalStateException is because of you not reading the element using iterator.next() before trying iterator.remove(). <br />So if you have your code like<br /><br />it.next();<br />while(it.hasNext()){<br />System.out.println("Done");<br />it.remove();<br />}<br />You will get the output as<br />Done<br />Done<br /><br />which clearly shows that the first loop or Anonymoushttps://www.blogger.com/profile/05241468218198086627noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-70415429351978859352017-02-04T22:33:31.067-08:002017-02-04T22:33:31.067-08:00seems like there is a bug with consistency of Conc...seems like there is a bug with consistency of ConcurrentModificationException... as if i write code like this,<br />if(s.equals("AC")){ al.remove("AC"); }<br />then while loop iterates 4 times...& at 4th time it throws Concurrent exception...<br />Ideally it should iterate 3 times only.<br />So i think dynamic state change of arraylist size is affecting consistency of Anonymoushttps://www.blogger.com/profile/11985669740225770750noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-73063040868213403302017-01-15T02:30:21.006-08:002017-01-15T02:30:21.006-08:00@Javin, Hi regarding your reply to james doubt. ac...@Javin, Hi regarding your reply to james doubt. actually there is an element with value "AB" and the cursor is entering the if block, the modCount is changing to 4 after removing element, but still it is not throwing ConcurrentModificationException exception . Please kindly explain it. moreover, it throws java.lang.IllegalStateException if i try to remove by using it.remove().<br /><Anonymoushttps://www.blogger.com/profile/04441632469134360119noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-77326863922964702672016-12-31T05:05:59.757-08:002016-12-31T05:05:59.757-08:00Hello james, because there would not be any string...Hello james, because there would not be any string which is equal to "AB" in your ArrayList, so it won't be going inside if block, you can check that by putting a System.out.println("removing element") in the if block.<br /><br />As soon as al.remove(1); is executed while iterating it will throw ConcurrentModificationException in Java.javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-89760372621554104812016-12-30T19:17:02.763-08:002016-12-30T19:17:02.763-08:00Hi Javin, thanks for your reply. But the question ...Hi Javin, thanks for your reply. But the question was I am getting ConcurrentModificationException only when I remove the "if" condition from the above code. It's running fine otherwise, why ?(I was trying some sample code and I came across it)Anonymoushttps://www.blogger.com/profile/11985263598726752172noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-7728484688571328632016-12-30T19:00:23.070-08:002016-12-30T19:00:23.070-08:00Hello @james, you are getting ConcurrentModificati...Hello @james, you are getting ConcurrentModificationException because you are calling ArrayList's remove method i.e. al.remove(1) inside the if block.<br /><br />While iterating you should always use Iterator's remove method to remove element e.g. it.remove(), this will remove current element, not the first one. <br /><br />See my post <a href="http://www.java67.com/2015/10/javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-56770282395185537222016-12-30T10:23:33.379-08:002016-12-30T10:23:33.379-08:00Can someone explain why the following code is not ...Can someone explain why the following code is not throwing ConcurrentModificationException ?<br /><br /> ArrayList al = new ArrayList();<br /> al.add("AA");<br /> al.add("AB");<br /> al.add("AC");<br /> Iterator it = al.iterator();<br /> while(it.hasNext()){<br /> String s = it.next();<br /> if(s.equals("AB")){<br /> al.remove(1);<br /> }<br Anonymoushttps://www.blogger.com/profile/11985263598726752172noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-89854463289872510902016-09-25T11:18:26.560-07:002016-09-25T11:18:26.560-07:00CopyOnWriteArrayList is fail-safe, it means it cre...CopyOnWriteArrayList is fail-safe, it means it create new copy of list when there is any changes in list. In that case how data is consistent between multiple threads if thread is operating on a separate copy of List.<br />Please explain Anonymoushttps://www.blogger.com/profile/15514218932139796375noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-9350470624293304492016-07-14T09:40:03.209-07:002016-07-14T09:40:03.209-07:00Nice blog, very helpful Nice blog, very helpful Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-79070310889869036742014-07-02T00:59:22.392-07:002014-07-02T00:59:22.392-07:00Very nice , when we use fail fast and fail safe i...Very nice , when we use fail fast and fail safe iterator ,? Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-250639961030368352014-06-06T21:12:36.039-07:002014-06-06T21:12:36.039-07:00Your code for Concurrent modification exception is...Your code for Concurrent modification exception is not showing itAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-34741199624600305512014-04-12T23:36:38.729-07:002014-04-12T23:36:38.729-07:00@Javin nice article one very important thing that ...@Javin nice article one very important thing that is missing Ithat I want to highlight is that..<br /><br />you look at the code for a Collection implementation, lets pick ArrayList; we have a modCount variable declared in AbstractList:<br /><br />protected transient int modCount = 0;<br /><br />And then in each and every modifying method (for example remove) for the ArrayList we have<br /><br />SARAL SAXENAhttps://www.blogger.com/profile/01084233786047386880noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-47586368022836185642014-02-01T01:19:39.606-08:002014-02-01T01:19:39.606-08:00public class FailFastTest {
public static void ma...public class FailFastTest {<br /> public static void main(String[] args) {<br /><br /> ArrayList nameList = new ArrayList();<br /> nameList.add("praveen");<br /> nameList.add("ravi");<br /> nameList.add("javarevisited");<br /> nameList.add("chinni");<br /> <br /> Iterator itr = nameList.iterator();<br /> while (itr.hasNext()) {<br /> String stringpraveenhttps://www.blogger.com/profile/06412485554423623405noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-58222988447478249312014-01-21T01:34:40.042-08:002014-01-21T01:34:40.042-08:00Hi
very nice article. But i would suggest you to ...Hi<br /><br />very nice article. But i would suggest you to correct the statement : "Structural changes means adding, removing or updating any element from collection while one thread is Iterating over that collection".<br /><br />Structural change does not include updating element. So, for example, while iterating over HashMap, if we update 'value' object for a key using put() Achint Mittalhttps://www.blogger.com/profile/07603328137891148165noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-81695448812108386422013-07-03T02:38:01.806-07:002013-07-03T02:38:01.806-07:00Hey mate, what is difference between fail-fast and...Hey mate, what is difference between fail-fast and weekly consistent iterator? does fail-safe and weekly consistent iterator refers to same thing? As you pointed out that iterators of many concurrent collections e.g. ConcurrentHashMap and CopyOnWriteArrayList are fail-safe, which I have read somewhere that they are also weekly consistent with original collection.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-48684388477765998802013-03-13T09:05:17.326-07:002013-03-13T09:05:17.326-07:00CopyOnWrite concept is useful only when thread nee...CopyOnWrite concept is useful only when thread need copy of collection for read purpose. Anonymoushttps://www.blogger.com/profile/14359910486647844886noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-37709555039594920102013-03-07T19:27:50.871-08:002013-03-07T19:27:50.871-08:001) Please give example code of ConcurrentModificat...1) Please give example code of ConcurrentModificationException<br />2) How it happens?<br />3) How we can avoid the ConcurrentModificationException in the code?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-18189170118857519802012-11-22T14:22:03.508-08:002012-11-22T14:22:03.508-08:00This comment has been removed by the author.Miteffhttps://www.blogger.com/profile/06507530914545040221noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-28560045277535824302012-08-19T06:35:20.173-07:002012-08-19T06:35:20.173-07:00Structural changes means adding or removing. Updat...Structural changes means adding or removing. Updating any element from collection is not structural change.<br /><br />Please check as I found on net at many places this definition. <br />skatarianoreply@blogger.com